表示除未定义之外的所有内容的 TypeScript 类型
TypeScript type representing everything but undefined
我有这样的功能:
export const logToStdout = function(v: NotUndefined){
console.log(JSON.stringify({
marker: '@json-stdio',
value: v
});
}
我想要的是 NotUndefined 的定义,除了 undefined
.
当运行:
时可以看到问题
console.log(JSON.stringify({value: ''}));
console.log(JSON.stringify({value: undefined}));
什么是漂亮的是:
export type NotUndefined = !undefined;
正确的做法是什么?
我在 Github 上为 TS 看到了这个相关问题:https://github.com/Microsoft/TypeScript/issues/7648
这个方法我试过了,没有报错:
在你抱怨之前,这里是code/gist:
https://gist.github.com/ORESoftware/e138f1840302c79b4ffc5f961337c44b
您需要启用严格的空值检查,否则 null
和 undefined
将被隐式添加为每种类型的有效值。
参考文献:
我有这样的功能:
export const logToStdout = function(v: NotUndefined){
console.log(JSON.stringify({
marker: '@json-stdio',
value: v
});
}
我想要的是 NotUndefined 的定义,除了 undefined
.
当运行:
时可以看到问题console.log(JSON.stringify({value: ''}));
console.log(JSON.stringify({value: undefined}));
什么是漂亮的是:
export type NotUndefined = !undefined;
正确的做法是什么?
我在 Github 上为 TS 看到了这个相关问题:https://github.com/Microsoft/TypeScript/issues/7648
这个方法我试过了,没有报错:
在你抱怨之前,这里是code/gist: https://gist.github.com/ORESoftware/e138f1840302c79b4ffc5f961337c44b
您需要启用严格的空值检查,否则 null
和 undefined
将被隐式添加为每种类型的有效值。
参考文献: