简体   繁体   English

如何使TS编译器知道未知变量具有某些属性(不使用“ any”类型)

[英]How to make the TS compiler know an unknown variable has some properties (without the usage of `any` type)

function f(): unknown {
    return {abc: "ABC"};
}

const a = f();

if (a && a instanceof Object && a.hasOwnProperty("abc")) {
    console.log(a.abc);
}

I have a variable a which is the unknown type at first (>= TypeScript 3.0). 我有一个变量a ,起初是unknown类型(> = TypeScript 3.0)。 I want to use the abc property of a inside an if statement when a has the property. 我想在a具有该属性aif语句中使用abc属性。

VS Code ts错误截图

However, as the above VS code screenshot shows, the TypeScript compiler gives the TS2339 error even though I checked a.hasOwnProperty("abc") in the if condition. 但是,如上面的VS代码屏幕截图所示,即使我在if条件下检查了a.hasOwnProperty("abc") ,TypeScript编译器TS2339出现TS2339错误。

Property 'abc' does not exist on type 'object'. 类型“对象”上不存在属性“ abc”。 ts(2339) TS(2339)

I know that I can bypass this error by casting a to any type, but it will make the compiler ignore the typos of members' names, and it will be bad if there are many properties in a and many things to do with the variable a . 我知道我可以通过铸造绕过这个错误aany类型的,但它将使编译器忽略的成员的姓名拼写错误,如果有许多特性将是糟糕的a ,很多事情做的变量a

Is there a way for the TypeScript compiler to know that a has certain properties? TypeScript编译器是否可以知道a具有某些属性?

You can use { [index: string]: unknown } instead of unknown to tell TypeScript your object is a dictionary of unknown properties. 您可以使用{ [index: string]: unknown }而不是unknown来告诉TypeScript您的对象是未知属性的字典。

declare const unfamiliar: { [index: string]: unknown };

if ('foo' in unfamiliar) {
  unfamiliar.foo;
}

The error comes from the assertion that a is an Object . 错误来自于断言aObject The Object type does not contain abc so the following assertion does not make a difference. Object类型不包含abc因此以下断言没有区别。
If you only keep the a.hasOwnProperty() assertion it should work. 如果仅保留a.hasOwnProperty()断言,则它应该起作用。

暂无
暂无

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

相关问题 如何制作一个Typescript接口,该接口类型检查某些属性,但也允许任何属性? - How can I make a Typescript interface which type checks some properties, but also allows any prorperty? 当变量不为null时,如何使Typescript编译器知道? - How to make Typescript compiler know when a variable is not null? 编译器错误 TS7053:元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“Igame” - Compiler Error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Igame' 如何创建需要某些属性但可以容忍任何其他属性的类型? - How to create a type that requires some properties but tolerates any additional properties? Typescript/nodejs:变量在某些位置隐含类型为“any” - Typescript/nodejs: variable implicitly has type 'any' in some locations TS 错误:类型“未知”不可分配给类型“FunctionComponent”<any> 由于带路由器 - TS error: Type 'unknown' is not assignable to type 'FunctionComponent<any> due to withRouter 如何在 TS 中键入未知对象数组? - How to type array of unknown objects in TS? 类型 &#39;{ 日期样式:字符串; }&#39; 与类型 &#39;DateTimeFormatOptions&#39;.ts(2559) 没有共同的属性 - Type '{ dateStyle: string; }' has no properties in common with type 'DateTimeFormatOptions'.ts(2559) 如何在 React 打字稿中使用镭(错误:与类型“CSSProperties”没有共同的属性。TS2559)? - How to use radium in React typescript (Error: has no properties in common with type 'CSSProperties'. TS2559)? 对象的类型为“未知”.ts - Object is of type 'unknown'.ts
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM