Skip to main content


Disallow returning a value with type any from a function.


该规则需要 类型信息 才能运行。

TypeScript 中的 any 类型是类型系统中危险的 "应急方案"。 使用 any 会禁用许多类型检查规则,通常最好仅作为最后手段或在构建代码原型时使用。

英:The any type in TypeScript is a dangerous "escape hatch" from the type system. Using any disables many type checking rules and is generally best used only as a last resort or when prototyping code.

尽管你的意图是好的,但 any 类型有时可能会泄漏到你的代码库中。 从函数返回 any 类型的值会在代码库中创建潜在的类型安全漏洞和错误来源。

英:Despite your best intentions, the any type can sometimes leak into your codebase. Returning an an any-typed value from a function creates a potential type safety hole and source of bugs in your codebase.

此规则不允许从函数返回 anyany[]

英:This rule disallows returning any or any[] from a function.

此规则还比较泛型类型参数类型,以确保你不会在泛型位置向需要特定类型的函数返回不安全的 any。 例如,如果从声明为返回 Set<string> 的函数返回 Set<any>,则会出错。

英:This rule also compares generic type argument types to ensure you don't return an unsafe any in a generic position to a function that's expecting a specific type. For example, it will error if you return Set<any> from a function declared as returning Set<string>.

module.exports = {
"rules": {
"@typescript-eslint/no-unsafe-return": "error"
在线运行试试这个规则 ↗


function foo1() {
return 1 as any;
function foo2() {
return Object.create(null);
const foo3 = () => {
return 1 as any;
const foo4 = () => Object.create(null);

function foo5() {
return [] as any[];
function foo6() {
return [] as Array<any>;
function foo7() {
return [] as readonly any[];
function foo8() {
return [] as Readonly<any[]>;
const foo9 = () => {
return [] as any[];
const foo10 = () => [] as any[];

const foo11 = (): string[] => [1, 2, 3] as any[];

// generic position examples
function assignability1(): Set<string> {
return new Set<any>([1]);
type TAssign = () => Set<string>;
const assignability2: TAssign = () => new Set<any>([true]);
Open in Playground

在某些情况下,规则允许将 any 返回到 unknown

英:There are cases where the rule allows to return any to unknown.

允许的 anyunknown 返回示例:

英:Examples of any to unknown return that are allowed:

function foo1(): unknown {
return JSON.parse(singleObjString); // Return type for JSON.parse is any.

function foo2(): unknown[] {
return [] as any[];
Open in Playground


如果你的代码库有许多现有的 any 或不安全代码区域,则可能很难启用此规则。 在项目的不安全区域中,在增加类型安全性之前,跳过 no-unsafe-* 规则可能会更容易。 你可以考虑在这些特定情况下使用 ESLint 禁用注释,而不是完全禁用此规则。

英:If your codebase has many existing anys or areas of unsafe code, it may be difficult to enable this rule. It may be easier to skip the no-unsafe-* rules pending increasing type safety in unsafe areas of your project. You might consider using ESLint disable comments for those specific situations instead of completely disabling this rule.