[英]What does it mean by the extra pipeline in the type declaration in typescript
Recently I've declared a type like-最近我宣布了一个类型——
interface SomeType {
property: {
a: number;
b: string;
} | undefined;
}
But when I saved the type, the vscode (maybe prettier) converts it into-但是当我保存类型时,vscode(可能更漂亮)将其转换为-
interface SomeType {
property:
| {
a: number;
b: string;
}
| undefined;
}
So, you see there is an extra |
所以,你看到有一个额外的|
symbol after the property
key. property
键后的符号。 So what exactly is it?那到底是什么呢? Can anyone describe the behavior?谁能描述这种行为?
Note: This only happens if I set property: {a: number; b: string;} | undefined;
注意:只有当我设置property: {a: number; b: string;} | undefined;
property: {a: number; b: string;} | undefined;
. . If the | undefined
如果| undefined
| undefined
is not set then everything is same as it is. | undefined
没有设置那么一切都是一样的。
The behavior is exactly the same as if there was no leading |
行为与没有前导|
完全相同。 . . It's simply a style choice.这只是一种风格选择。 It allows, for example例如,它允许
let foo:
| 'a'
| 'b'
| 'c';
whereas if the syntax did not permit the leading |
而如果语法不允许前导|
, it'd have to look like ,它必须看起来像
let foo:
'a'
| 'b'
| 'c';
which might grind some people's gears.这可能会磨伤某些人的齿轮。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.