gpt4 book ai didi

typescript 将null分配给接口(interface)

转载 作者:行者123 更新时间:2023-12-05 00:45:53 24 4
gpt4 key购买 nike

这可能是一个愚蠢的问题,但我不知道如何将 null 的值设置为声明为接口(interface)类型的变量。

下面是我的意思的一个例子:

interface IMyInterface {
field: any;
}

let test:IMyInterface = null; // <-- Compiler error: Type 'null' is not assignable to type 'IMyInterface'

最佳答案

要么将其定义为联合类型,要么在 compilerOptions 下的 ts-config 中将 strictNullChecks 标志设置为 false。

@Guy 隐身:

let test:IMyInterface | null = null;

关于 typescript 将null分配给接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62576577/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com