Hey all I'm trying to dynamically set a style interface to a type like this:
type IListStyles = {
[LIST_TYPE.ADD_ACTIONABLE]: IListItemAddActionableStyle;
[LIST_TYPE.SUPERVALUE]: IListItemSupervalueStyle;
}
enum LIST_TYPE {
ADD_ACTIONABLE = "ADD_ACTIONABLE",
SUPERVALUE = "SUPERVALUE",
}
export interface IListItem<T extends LIST_TYPE> {
style?: IListStyles[T];
type: T;
Buuut I'm getting that T can't be used to index IListStyles
has anyone done something like this before?
you can also use an object instead of enum:
const LIST_TYPE = {
ADD_ACTIONABLE: "ADD_ACTIONABLE",
SUPERVALUE: "SUPERVALUE",
} as const;
type TLISTTYPE = typeof LIST_TYPE[keyof typeof LIST_TYPE];
interface IListItemAddActionableStyle {
a: string;
}
interface IListItemSupervalueStyle {
b: string;
}
type TListStyles = {
[LIST_TYPE.ADD_ACTIONABLE]: IListItemAddActionableStyle;
[LIST_TYPE.SUPERVALUE]: IListItemSupervalueStyle;
};
export interface IListItem<T extends TLISTTYPE> {
style?: TListStyles[T];
type: T;
}
const resu: IListItem<"ADD_ACTIONABLE"> = {
type: "ADD_ACTIONABLE",
style: { a: "dskqldq" },
};
console.log({ resu });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.