Home > other >  how to make one type of another?
how to make one type of another?

Time:11-28

how can I make the values of "Text Block Type" the keys of "IFormats", which may not be

type TextBlockType = 'markdown' | 'test' | 'presentation';

interface IFormats {
  [index: TextBlockType]: string;
}

I did it like this

type FormatsType = {
    [key in TextBlockType]: string
}

I want to get

type FormatsType = {
    markdown?: string
    test?: stirng
    presentation?: string
}

CodePudding user response:

You were pretty close:

type TextBlockType = 'markdown' | 'test' | 'presentation';

type FormatsType = {
    [key in TextBlockType]?: string
}

Alternatively, you can use Record and Partial utility types:

type FormatsType = Partial<Record<TextBlockType, string>>
  • Related