gpt4 book ai didi

typescript - 为什么 TypeScript 允许滥用 String 和 string?

转载 作者:搜寻专家 更新时间:2023-10-30 21:50:05 25 4
gpt4 key购买 nike

为什么 TypeScript 允许我将字符串基元分配给类型为 String(对象类型)的变量?例如,为什么这不会导致错误:

var str : String = '2333';

请注意,另一种方式不起作用:

var str : string = new String('2333'); // Error: Type 'String' is not assignable to type 'string'. 'string' is a primitive, but 'String' is a wrapper object. Prefer using 'string' when possible.

代码如下:

const str: string = '2333'
console.log(`typeof str === "string" is ${typeof str === "string"}`)

const str2: String = new String('2333')
console.log(`str2 instanceof String is ${str2 instanceof String}`)
console.log(`typeof str2 === "string" is ${typeof str2 === "string"}`)

const str3: String = '2333'
console.log(`str3 instanceof String is ${str3 instanceof String}`)
console.log(`typeof str3 === "string" is ${typeof str3 === "string"}`)

它说:

typeof str === "string" is true
str2 instanceof String is true
typeof str2 === "string" is false
str3 instanceof String is false
typeof str3 === "string" is true

strstr2我们可以知道stringString是不同的类型。为什么 str3 允许滥用两种类型?

最佳答案

简短版本:TypeScript 特别允许将 string 分配给 String

长版本:TypeScript 允许将字符串、数字或 bool 基元分配给其关联对象类型的变量。这在 §3.11.4 节中定义和 §3.11.1规范。

§3.11.4说:

S is assignable to a type T, and T is assignable from S, if S has no excess properties with respect to T (3.11.5) and one of the following is true:

  • S is an object type, an intersection type, an enum type, or the Number, Boolean, or String primitive type, T is an object type, and for each member M in T, one of the following is true:
    • M is a property and S has an apparent property N where
      • M and N have the same name,
      • the type of N is assignable to that of M,
      • if M is a required property, N is also a required property, and
      • M and N are both public, M and N are both private and originate in the same declaration, M and N are both protected and originate in the same declaration, or M is protected and N is declared in a class derived from the class in which M is declared.
    • M is an optional property and S has no apparent property of the same name as M.
    • M is a non-specialized call or construct signature and S has an apparent call or construct signature N where, when M and N are instantiated using type Any as the type argument for all type parameters declared by M and N (if any),
      • the signatures are of the same kind (call or construct),
      • M has a rest parameter or the number of non-optional parameters in N is less than or equal to the total number of parameters in M,
      • for parameter positions that are present in both signatures, each parameter type in N is assignable to or from the corresponding parameter type in M, and
      • the result type of M is Void, or the result type of N is assignable to that of M.
    • M is a string index signature of type U, and U is the Any type or S has an apparent string index signature of a type that is assignable to U.
    • M is a numeric index signature of type U, and U is the Any type or S has an apparent string or numeric index signature of a type that is assignable to U.

然后 §3.11.1说:

The apparent members of the primitive type String and all string literal types are the apparent members of the global interface type 'String'.

提取相关的部分:

S (string in our case) is assignable to a type T (String in our case)...if S has no excess properties with respect to T, ... S is...the Number, Boolean, or String primitive type, T is an object type...and for each member M in T...M is a property and S has an apparent property N where...M and N have the same name; the type of N is assignable to that of M; if M is a required property, N is also a required property; and M and N are both public...

...因为 string 的明显成员是 String 的明显成员。

其他部分§3.11以及其他各个部分(例如 §3.2.3)也涉及原始类型与其关联对象类型之间的关系。

关于typescript - 为什么 TypeScript 允许滥用 String 和 string?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46326385/

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