Suppose I have declared a type alias in TypeScript like this:
export type PlatformType = 'em' | 'ea' | 'hi';
How can I specify a property in an interface that only accepts an array containing one instance of each PlatformType
? For instance, I would like to achieve the following:
export interface Constants {
platformTypes: PlatformType[]; // what is the correct way to define the type for platformTypes?
}
When creating an object of type Constants
, it should adhere to this structure:
export const constants: Constants = {
platformTypes: ['em', 'ea', 'hi']
}
Essentially, I want to trigger an error in scenarios such as:
export const constants: Constants = {
platformTypes: ['em', 'ea'] // should result in an error as 'hi' is missing
}
or
export const constants: Constants = {
// This will throw an error since 'extra' is not part of the PlatformType alias
platformTypes: ['em', 'ea', 'hi', 'extra']
}
or
export const constants: Constants = {
platformTypes: ['em', 'em', 'ea', 'hi'] // should trigger an error due to duplicate 'em'
}