简体   繁体   English

为什么任何可以分配给 TypeScript 中的每种类型

[英]Why can any be assigned to every type in TypeScript

This is a philosophical question about the type structure in TypeScript.这是关于 TypeScript 中类型结构的哲学问题。

Let's take look at the following short snippet ( available as a playground here ).让我们看一下下面的简短片段( 可在此处作为游乐场使用)。

function f(): string {
    if (Math.random() > 0.5) {
        return 'a real string';
    }
    var x: any = null;
    // Why does this type check?
    return x;
}

const myString = f();
// myString has type string
// not string | any, not string | undefined, not string | null
// so I expect it to really be a string
console.log(myString.length);
// -> TypeError: null is not an object (evaluating myString.length)

In short, my question is why does this example compile with all possible strictness flags enabled?简而言之,我的问题是为什么这个例子编译时启用了所有可能的严格标志?

The documentation on any states that any is used to "describe the type of variables that we do not know". any any “描述我们不知道的变量的类型”。 For this reason, it makes sense that all types can be assigned to any , however, why can any be assigned to all other types?出于这个原因,所有类型都可以分配给any是有道理的,但是,为什么any可以分配给所有其他类型?

By its very nature, objects with type any have unknown properties, so why can they be marshaled into other types (like string ) with no warning?就其本质而言,类型为any的对象具有未知属性,那么为什么它们可以在没有警告的情况下被编组为其他类型(如string )? Does this not defeat the purpose of explicitly tagging things with types when we know the compiler is not enforcing their contents?当我们知道编译器没有强制执行它们的内容时,这是否不会破坏使用类型显式标记事物的目的?

For example, let's say that I want to define a function that unequivocally returns a string (not null , not undefined , not number , but exactly string ).例如,假设我想定义一个明确返回string的 function (不是null ,不是undefined ,不是number ,而是确切地string )。 Is there a way to get the TypeScript compiler to enforce that every codepath that returns from that function returns a string?有没有办法让 TypeScript 编译器强制从该 function 返回的每个代码路径都返回一个字符串? My preconception is that the example above should be doing that, and that we should see an error like any is not assignable to type string , but we do not.我的先入之见是上面的示例应该这样做,并且我们应该看到一个错误,例如any is not assignable to type string ,但我们没有。

The documentation on any states that any is used to "describe the type of variables that we do not know" any 的文档说明 any 用于“描述我们不知道的变量的类型”

Since TypeScript 3.0, you should use unknown to describe such types instead.由于 TypeScript 3.0,您应该使用unknown来描述这些类型。

any is an artifact from the past, and you shouldn't be using it except for rare edge cases where you cannot use unknown . any是过去的产物,你不应该使用它,除非你不能使用unknown的罕见边缘情况。 There are a few (arcane) ones.有几个(奥术)。

Solution解决方案

function f(): string {
    if (Math.random() > 0.5) {
        return 'a real string';
    }
    var x: unknown = null;
    return x; // Compile-time error: "Type 'unknown' is not assignable to type 'string'.(2322)"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么jest.fn()可以赋值给Typescript中任意一个function? - Why jest.fn() can be assigned to any function in Typescript? 为什么不先显式地强制将“ any”分配给任何类型? - Why can 'any' be assigned to any type without explicitly casting it first? 在TypeScript中,为什么不能将参数化函数分配给无参数函数类型? - In TypeScript why can't a parameterized function be assigned to a paramaterless function type? 可以将 TypeScript 类型谓词分配给变量吗 - Can a TypeScript type predicate be assigned to a variable Typescript - eslint 错误分配给类型的“任何”类型的不安全参数 - Typescript - eslint error Unsafe argument of type 'any' assigned to a type 在 TypeScript 中,为什么函数返回类型没有按预期分配给泛型类型? - In TypeScript, why is the function return type not assigned to generic type as expected? TypeScript integer 枚举对象可以分配任何值 - TypeScript integer enum objects can be assigned any value 来自泛型类型的查找类型不能分配给“任何” - Lookup type from generic type can't be assigned to “any” void 类型的 Function 不能分配给 Observable 类型<any></any> - Function of type void can't be assigned to type Observable<any> 为什么TypeScript中HTMLScriptElement上的一些属性不能赋值? - Why can't some of the properties on the HTMLScriptElement be assigned to in TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM