这类似于#40796374,但这是围绕类型,而我使用接口。

给定下面的代码:

接口Foo { 名称:字符串; } 函数go() { 让实例:Foo | null = null; Let mutator = () => { 实例= { 名称:“字符串” }; }; mutator (); If (instance == null) { console.log('实例为空或未定义'); }其他{ console.log (instance.name); } }

我有一个错误说'属性'名称'不存在类型'从不'。

我不明白实例怎么会是一个“永远”。有人能解释一下吗?


当前回答

我有什么问题吗?和!

这件作品对我很有用。

if (ref != null){
    if (ref.current != null)
        ref.current.appendChild(child);
}

其他回答

如果你在参数中收到错误,那么保持任何或任何[]类型的输入,如下所示

getOptionLabel={(option: any) => option!.name}
 <Autocomplete
    options={tests}
    getOptionLabel={(option: any) => option!.name}
    ....
  />

如果你把Component写成React。FC,使用useState(),

这样写会很有帮助:

const [arr, setArr] = useState<any[]>([])

在我的例子中,发生这种情况是因为我没有输入变量。

所以我创建了搜索界面

export interface Search {
  term: string;
  ...
}

我改变了

searchList = [];

为此,它起作用了

searchList: Search[];

当我忘记为变量定义类型时,就发生了这种情况。

我有同样的错误,把点符号换成了括号符号来抑制它。

例如:

obj.name -> obj['name']