[英]Why setting object of “any” type to property with specific type doesn't throw TS compilation error?
Why this code is not throwing any Typescript errors (see TS playground ): 为什么此代码没有引发任何Typescript错误(请参阅TS操场 ):
interface ErrorData {
code: number;
message: string;
}
interface EnrichedError {
description: string;
error: ErrorData;
}
const a: any = "bla bla bla";
const b: EnrichedError = {
description: 'this is a test',
error: a
}
Why it's possible to set b.error
to a? 为什么可以将b.error
设置为a?
Because that's the point of any
, it is assignable to all types, and all types are assignable to it. 因为这就是any
的要点,所以它可以分配给所有类型,并且所有类型都可以分配给它。
It is your escape hatch, it basically tells TypeScript that "this value can go anywhere" 这是您的逃生舱口,它基本上告诉TypeScript“此值可以随处可见”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.