让我们有the following code in TypeScript:
function f<T>(a: T, b: T) { }
f("a", 1);
TypeScript可能会失败,并显示以下信息:
'number'类型的参数不能分配给'string'类型的参数。
但是,when I pass null
(or undefined
) instead of 1
的TypeScript将通用参数<T>
的类型更改为string | null
,并且不会失败。
我有两个问题:
null
或undefined
与例如number
?string | null
)?答案 0 :(得分:2)
为什么null或undefined与e.g.数字?
说实话,我不确定100%为什么。如评论中所述,它推断a
的类型为'string',但允许'null |未定义。我做了一些实验,却不知道为什么要这么做。
以及如何防止扩展(将类型更改为字符串| null)? 您可以做什么的一些示例:
// Original
function f<T>(a: T, b: T) { }
f('a', 'b') // Allowed
f(1, 2) // Allowed
f(true, false) // Allowed
f("a", 1); // Has error
f("a", undefined); // Allowed
f("a", null); // Allowed
f(null, null) // Allowed
// Extending then defining
// Will only allow strings
function g<T extends string = string>(a: T, b: T) { }
g('a', 'b') // Allowed
g(1, 2) // Has error
g(true, false) // Has error
g("a", 1); // Has error
g("a", undefined); // Has error
g("a", null); // Has error
g(null, null) // Has error
// Using Required
// Does not allow null or undefined but allows other non strings
function x<T>(a: Required<T>, b: Required<T>) { }
x('a', 'b') // Allowed
x(1, 2) // Allowed
x(true, false) // Allowed
x("a", 1); // Has error
x("a", undefined); // Has error
x("a", null); // Has error
x(null, null) // Has error