可以说我们有两个接口:
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 ×1