相关疑难解决方法(0)

打字稿中类型[]和[类型]之间的区别

可以说我们有两个接口:

interface WithStringArray1 {
    property: [string]
}

interface WithStringArray2 {
    property: string[]
}
Run Code Online (Sandbox Code Playgroud)

让我们声明一些这些类型的变量:

let type1:WithStringArray1 = {
   property: []
}

let type2:WithStringArray2 = {
    property: []
}
Run Code Online (Sandbox Code Playgroud)

第一次初始化失败了:

TS2322: Type '{ property: undefined[]; }' is not assignable to type 'WithStringArray1'.
Types of property 'property' are incompatible.
Type 'undefined[]' is not assignable to type '[string]'.
Property '0' is missing in type 'undefined[]'.
Run Code Online (Sandbox Code Playgroud)

第二个是好的.

[string]和之间有什么区别string[]

typescript

17
推荐指数
1
解决办法
2435
查看次数

标签 统计

typescript ×1