使用const storage : Readonly<{id: string}[]>
,如何在storage
中的静态id
中键入一些string
?该数组在运行时不会更改,并且被硬编码在中.
我试过(typeof storage)[number]['id']
,但只有string
.也试过satisfies Readonly<{id:string}[]>
interface HasID {
id: string
info?: any
}
const storage : Readonly<HasID[]> = [
{ id: 'foo' },
{ id: 'bar', info: ['baz'] }
]
const storage2 = [
{ id: 'foo' },
{ id: 'bar', info: ['baz'] }
] satisfies Readonly<HasID[]>
let str // looking to type this to be "foo" | "bar"
type StorageID = (typeof storage)[number]['id'] // string
type StorageID2 = (typeof storage2)[number]['id'] // string