Reputation: 80
In the example below, is there a way to tell typescript that arr2 is exactly the type it's initialized as?
let arr1 = [{pass: "YAY"}];
arr1[0].pass
let arr2 = [{fail: "NOO"}, 'ok'];
arr2[0].fail //compiler error
Upvotes: 1
Views: 40
Reputation: 371019
as const
will do what you need. It'll type the arr2
as a tuple (containing an object with a fail
property, and containing an 'ok'
string), and not as an array:
let arr2 = [{fail: "NOO"}, 'ok'] as const;
Upvotes: 2