#typescript #types #typescript-typings
#машинописный текст #типы #машинопись-наброски
Вопрос:
я хочу получить тип массива в некотором свойстве и получить автозаполнение с помощью набора текста
const arr = [{test:'option 1'},{test: 'option 2'}] type test = arr[number]['test'] let t:test = '' // will equal 'option 1' or 'option 2'
Ответ №1:
вам нужно привести arr к const, чтобы typescript мог правильно определить свой тип, например
const arr = [{test:'option 1'},{test: 'option 2'}] as const; // lt;-- cast to const here type test = typeof arr[number]['test']; let t:test = ''; // will equal 'option 1' or 'option 2'