Tenemos:
type test = { id: number; name: string; }; const arr: test[] = [{ id: 1, name: 'test-1' }, { id: 2, name: 'test-2' }];Y quiero crear un tipo a partir de una matriz de objetos:
type other = typeof arr[number]['name']; const another: other = 'test-1'; // Should be 'test-1' | 'test-2' but it's string Si elimino el tipo personalizado de arr y uso la afirmación const:
const arr = [{ id: 1, name: 'test-1' }, { id: 2, name: 'test-2' }] as const; type other = typeof arr[number]['name']; const another: other = 'test-2'; // Works! Funcionará, pero también quiero tener un tipo para mi arr .
¿Cómo puedo crear algo como esto:
type test = { id: number; name: string; }; const arr: test[] = [ { id: 1, name: 'test-1' }, { id: 2, name: 'test-2' } ] as const; type other = typeof arr[number]['name']; const another: other = 'test-1'; ¡Intenté usar ReadonlyArray<test> pero no tuve suerte!