这类似于#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);
}

其他回答

在你的组件中。Ts文件只是像下面那样声明数组

public arrayName = ([] as any[]);

它和我一起为Angular工作过

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

所以我创建了搜索界面

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

我改变了

searchList = [];

为此,它起作用了

searchList: Search[];

这似乎类似于这个问题:当使用strictNullChecks在回调中更改值时,False“属性不存在于类型'never'”,这是这个问题(讨论)的副本:控制流分析中的权衡。

这个讨论很长,如果你找不到好的解决方案,你可以试试这个:

if (instance == null) {
    console.log('Instance is null or undefined');
} else {
    console.log(instance!.name); // ok now
}

我有什么问题吗?和!

这件作品对我很有用。

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

对我来说,以下代码是工作的:

const [disc, setDisc] = useState<any[]>([]);

到目前为止,我所看到的是:当你没有指定传递给useState()的类型时,它推断它永远是类型。 通过使用any[],您可以在请求时传递数据。