typescript “未知”与“任何”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51439843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 07:55:20  来源:igfitidea点击:

'unknown' vs. 'any'

typescripttypescript3.0

提问by Jacoslaw

TypeScript 3.0 introduces unknowntype, according to their wiki:

unknown根据他们的 wiki,TypeScript 3.0 引入了类型:

unknown is now a reserved type name, as it is now a built-in type. Depending on your intended use of unknown, you may want to remove the declaration entirely (favoring the newly introduced unknown type), or rename it to something else.

未知现在是保留类型名称,因为它现在是内置类型。根据您对未知的预期用途,您可能希望完全删除声明(支持新引入的未知类型),或将其重命名为其他名称。

What is difference between unknownand any? When should we use unknownover any?

unknown和 和有什么区别any?我们什么时候应该使用unknownover any

回答by Titian Cernicova-Dragomir

You can read more about unknownin the PRor the RC announcement, but the gist of it is:

您可以unknownPRRC 公告中阅读更多信息,但其要点是:

[..] unknown which is the type-safe counterpart of any. Anything is assignable to unknown, but unknown isn't assignable to anything but itself and any without a type assertion or a control flow based narrowing. Likewise, no operations are permitted on an unknown without first asserting or narrowing to a more specific type.

[..] 未知哪个是 any 的类型安全对应物。任何东西都可以分配给 unknown,但 unknown 不能分配给任何东西,但它本身和任何没有类型断言或基于控制流的缩小。同样,如果没有首先断言或缩小到更具体的类型,则不允许对未知进行任何操作。

A few example:

几个例子:

let vAny: any = 10;          // We can assign anthing to any
let vUnknown: unknown =  10; // We can assign anthing to unknown just like any 


let s1: string = vAny;     // Any is assigable to anything 
let s2: string = vUnknown; // Invalid we can't assign vUnknown to any other type (without an explicit assertion)

vAny.method();     // ok anything goes with any
vUnknown.method(); // not ok, we don't know anything about this variable

The suggested usage is:

建议的用法是:

There are often times where we want to describe the least-capable type in TypeScript. This is useful for APIs that want to signal “this can be any value, so you must perform some type of checking before you use it”. This forces users to safely introspect returned values.

很多时候我们想在 TypeScript 中描述能力最低的类型。这对于想要发出信号“这可以是任何值,因此您必须在使用之前执行某种类型的检查”的 API 很有用。这迫使用户安全地内省返回值。

回答by Awad Maharoof

The difference between unknown and any is described as:

未知和任意之间的区别描述为:

Much like any, any value is assignable to unknown; however, unlike any, you cannot access any properties on values with the type unknown, nor can you call/construct them. Furthermore, values of type unknowncan only be assigned to unknownor any.

就像any,任何值都可以分配给unknown; 但是,与 不同的是any,您不能访问类型为值的任何属性unknown,也不能调用/构造它们。此外, type 的值unknown只能分配给unknownany

To answer your question of when should you use unknownover any:

要回答您何时应该使用unknownover 的问题any

This is useful for APIs that want to signal “this can be any value, so you mustperform some type of checking before you use it”. This forces users to safely introspect returned values.

这对于想要发出信号“这可以是任何值,因此您必须在使用之前执行某种类型的检查”的 API 很有用。这迫使用户安全地内省返回的值。

Take a look at the TypeScript 3.0 announcementfor examples of type checking a variable of type unknownand a more detailed explanation.

查看TypeScript 3.0 公告,了解检查类型变量的示例unknown和更详细的解释。

回答by Willem van der Veen

anytype:

any类型:

The anytype represents all possible JS values. Every type is assignable to type any. Therefore the type anyis an universal supertype of the type system. The TS compiler will allow any operation on values typed any. For example:

any类型代表所有可能的JS值。每种类型都可以分配给 type any。因此类型any是类型系统的通用超类型。TS 编译器将允许对输入的值进行任何操作any。例如:

let myVar: any;

myVar[0];
myVar();
myVar.length;
new myVar();

In many occasions this is too lenient of the TS compiler. i.e. it will allow operations which we could have known to be resulting into a runtime error.

在许多情况下,这对 TS 编译器来说太宽容了。即它将允许我们可能知道会导致运行时错误的操作。

unknowntype:

unknown类型:

The unknowntype represents (just like any) all possible JS values. Every type is assignable to type unknown. Therefore the type unknownis another universal supertype of the type system (alongside any). However, the TS compiler won'tallow any operation on values typed unknown. Furthermore, the unknowntype is only assignable to the type any. An example will clarify this:

unknown类型代表(就像any)所有可能的JS值。每种类型都可以分配给 type unknown。因此类型unknown是类型系统的另一个通用超类型(与 一起any)。然而,TS编译器不会允许在输入值的任何操作unknown。此外,unknown类型只能分配给 type any。一个例子将阐明这一点:

let myVar: unknown;

let myVar1: unknown = myVar;   // No error
let myVar2: any = myVar;       // No error
let myVar3: boolean = myVar;   // Type 'unknown' is not assignable to type 'boolean'

// The following operations on myVar all give the error:
// Object is of type 'unknown'
myVar[0];
myVar();
myVar.length;
new myVar();