Array<T>
vs T[]
These two syntaxes are very similar, and are almost functionally identical in [[ts]]. The only real difference is the use of [[20210331123346-keyof-typescript]] operator.
type Person = { id: string; name: string; }; const result: keyof Person[] = ["id", "name"]; // Type 'string[]' is not assignable to type 'keyof Person[]'.
The []
operator occurs before the keyof
operator. Meaning it evaluates as keyof Person[]
, not ('id' | 'name')[]
. You can fix this with parenthesis, or using the generic syntax.
const result: (keyof Person)[] = ["id", "name"]; const result: Array<keyof Person> = ["id", "name"];
They're so similar in fact, https://typescript-eslint.io/rules/array-type/ lets you pick one or the other for its rule.