12
phép nói rằng chúng tôi có hai giao diện:Sự khác nhau giữa loại [] và [loại] trong nguyên cảo
interface WithStringArray1 {
property: [string]
}
interface WithStringArray2 {
property: string[]
}
Cho phép khai báo một số biến của các loại sau:
let type1:WithStringArray1 = {
property: []
}
let type2:WithStringArray2 = {
property: []
}
Các khởi động đầu tiên không thành công với:
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[]'.
Điều thứ hai là ok.
Sự khác nhau giữa [string]
và string[]
là gì?