英文:
Why concatenating string and undefined does Not raise an error in TypeScript
问题
例如,这段代码无效:
function fn1(a: number, b?: number) {
console.log(a + b); // error 'b' is possibly 'undefined'.(18048)
}
fn1(5);
但这段代码是有效的:
function fn2(a: string, b?: string) {
console.log(a + b); // valid code
}
fn2('Hi');
为什么在严格模式下 TypeScript 在第二种情况下没有引发错误?
英文:
For example, this code is not valid:
function fn1(a: number, b?: number) {
console.log(a + b); // error 'b' is possibly 'undefined'.(18048)
}
fn1(5);
But this code is valid:
function fn2(a: string, b?: string) {
console.log(a + b); // valid code
}
fn2('Hi');
Why doesn't Typescript in strict mode raise an error in the second case?
答案1
得分: 1
这个问题的权威答案可以在 Microsoft TypeScript 团队的开发负责人在 microsoft/TypeScript#2093 的一个评论中找到:
在很多代码中都有这样的操作:
console.log("Foo is " + x);
其中
x
可能是一个函数,undefined
或null
,在这种情况下显式调用toString()
将不是一个好主意。我们决定,向字符串添加任何东西是如此常见,以至于它永远不会是一个错误。[这个建议] 不符合引入语言中的破坏性更改的标准。
所以答案就是:不管你是否同意,TypeScript 团队不想破坏现有代码,其中会将各种东西连接到字符串上。
在该问题以及类似的问题(例如 microsoft/TypeScript#7989、microsoft/TypeScript#42983 和 microsoft/TypeScript#48962)中提到,那些真正希望看到这个功能并愿意使用 ESLint 的人可以启用 the @typescript-eslint/restrict-plus-operands
规则,该规则将在使用 +
连接不同类型的值时发出错误。因此,即使它们不是 TypeScript 的直接组成部分,仍然有选择。
英文:
The authoritative answer to this question can be found in a comment on microsoft/TypeScript#2093 by the development lead of the TypeScript team at Microsoft:
> There's a huge amount of code out there that does stuff like this:
>
> console.log("Foo is " + x);
>
> where x
might be a function, undefined
or null
, in which case explicitly calling toString()
would be a bad idea. We made the decision that adding anything to a string is so often done that it is not ever an error.
>
> [This suggestion] doesn't meet the bar for introducing a breaking change in the language.
So that's the answer: whether or not you agree with it, the TypeScript team didn't want to break existing real-world code that concatenates random stuff to strings.
In that issue and similar issues (such as microsoft/TypeScript#7989, microsoft/TypeScript#42983, and microsoft/TypeScript#48962), it is mentioned that those who really want to see this functionality and are willing to use ESLint could enable the @typescript-eslint/restrict-plus-operands
rule which will issue errors when using +
to concatenate values of different types. So there are options out there, even if they are not part of TypeScript directly.
答案2
得分: 0
因为在拼接中,如果第一个值是string
,JavaScript会强制第二个值也是string
,这就是为什么编译器不关心b
可能是未定义的,因为你可以将任何东西与string
拼接。
然而,如果你尝试将a
的类型设为number
,或进行任何其他操作,就会出现错误:
function fn2(a: number, b?: string) {
console.log(a + b); // 'b' is possibly 'undefined'
}
fn2(5);
英文:
Because in concatenation, if the first value is string
JavaScript will force the second value to be string
that is why the compiler does not care if b
may be undefined, since you can concatenate anything with a string
.
However, if you try to make a
of type number
, or try any other operation the error will occur :
function fn2(a: number, b?: string) {
console.log(a + b); // 'b' is possibly 'undefined'
}
fn2(5);
答案3
得分: 0
我有不同的看法
我认为字符串 + 未定义是可以接受的,因为这是一个有效的JS操作
但数字不能与未定义连接,让我感到惊讶,因为这也是一个有效的JS操作
英文:
I have a different thought
I think string + undefined is acceptable because it is a valid JS operation
but number cannot concatenate undefined surprise me because it is also a valid JS operation
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论