Reputation: 1
there is my code
interface page {
type: '1' | '2'
}
interface Menu {
id: string,
title: string,
page: page,
}
export const menus: Menu[] = [
{
id: 'externalCustomer',
title: 'title',
page: { type: '1' }
}
]
is there any way to make id be const? the menus type is Menu[], and menu id is the const, just like
interface page {
type: '1' | '2'
}
interface Menu<T> {
id: T[number].id,
title: string,
page: page,
}
export const menus: Menu<typeof menus>[] = [
{
id: 'externalCustomer',
title: 'title',
page: { type: '1' }
}
]
i try to write it like
const menus = [
{
id: 'externalCustomer',
title: 'title',
page: { type: '1' }
}
] as const
but in this way, the type of page will be lose
Upvotes: 0
Views: 134
Reputation: 150
Using union type might does the job.
interface page {
type: '1' | '2'
}
interface Menu<I> {
id: I,
title: string,
page: page,
}
const menus: Menu<'externalCustomer' | 'test'>[] = [
{
id: 'externalCustomer',
title: 'title',
page: { type: '1' }
}, {
id: 'test',
title: 'title',
page: { type: '1' }
}, {
id: 'aaa', //type error
title: 'title',
page: { type: '1' }
},
];
Upvotes: 1