typescript '属性不存在于类型'从不'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44147937/
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
'Property does not exist on type 'never'
提问by Ray Booysen
This is similar to #40796374but that is around types, while I am using interfaces.
这与#40796374类似,但这是关于类型的,而我使用的是接口。
Given the code below:
鉴于以下代码:
interface Foo {
name: string;
}
function go() {
let instance: Foo | null = null;
let mutator = () => {
instance = {
name: 'string'
};
};
mutator();
if (instance == null) {
console.log('Instance is null or undefined');
} else {
console.log(instance.name);
}
}
I have an error saying 'Property 'name' does not exist on type 'never'.
我有一个错误,说“属性”名称在“从不”类型上不存在。
I don't understand how instance could ever be a 'never'. Can anyone shed some light on this?
我不明白实例怎么可能是“从不”。任何人都可以对此有所了解吗?
Thanks in advance.
提前致谢。
采纳答案by Saravana
Because you are assigning instance
to null
. The compiler infers that it can never be anything other than null
. So it assumes that the else block should never be executed so instance
is typed as never
in the else block.
因为您正在分配instance
给null
. 编译器推断它永远不会是null
. 所以它假设 else 块永远不应该被执行,所以instance
就像never
在 else 块中一样输入。
Now if you don't declare it as the literal value null
, and get it by any other means (ex: let instance: Foo | null = getFoo();
), you will see that instance
will be null
inside the if block and Foo
inside the else block.
现在,如果你不声明为文本值null
,和通过任何其他方式得到它(例如:let instance: Foo | null = getFoo();
),你会看到,instance
会null
的,如果块内Foo
的其他块内。
Never type documentation: https://www.typescriptlang.org/docs/handbook/basic-types.html#never
永远不要输入文档:https: //www.typescriptlang.org/docs/handbook/basic-types.html#never
Edit:
编辑:
The issue in the updated example is actually an open issue with the compiler. See:
更新示例中的问题实际上是编译器的未解决问题。看:
https://github.com/Microsoft/TypeScript/issues/11498https://github.com/Microsoft/TypeScript/issues/12176
https://github.com/Microsoft/TypeScript/issues/11498 https://github.com/Microsoft/TypeScript/issues/12176
回答by Nitzan Tomer
This seems to be similar to this issue: False "Property does not exist on type 'never'" when changing value inside callback with strictNullChecks, which is closed as a duplicate of this issue (discussion): Trade-offs in Control Flow Analysis.
这似乎与此问题类似:False "Property does not exist on type 'never'" 当使用 strictNullChecks 更改回调内的值时,该问题已作为此问题的副本(讨论)关闭:控制流分析中的权衡。
That discussion is pretty long, if you can't find a good solution there you can this:
那个讨论很长,如果你找不到一个好的解决方案,你可以这样做:
if (instance == null) {
console.log('Instance is null or undefined');
} else {
console.log(instance!.name); // ok now
}
回答by Eric Grotke
I had the same error and replaced the dot notation with bracket notation to suppress it.
我有同样的错误,并用括号表示法替换了点表示法以抑制它。
e.g.: obj.name -> obj['name']
例如:obj.name -> obj['name']
回答by blue-hope
if you write Component as React.FC, and using useState(),
如果你把组件写成 React.FC,并使用 useState(),
just write like this would be helpful:
像这样写会有所帮助:
const [arr, setArr] = useState<any[]>([])