DDDDDD
DDDDDD

Reputation: 61

How to use key in Type two times

Is it possible to use key in Type two times in a type?

For example:

type UnionTypeOne = '1' | '2'
type UnionTypeTwo = '3' | '4'

export type MyTime = {
  [key in UnionTypeOne]?: {
    [name: string]: boolean
  }
  [key in UnionTypeTwo]: boolean
}

Right now the compilation fails because the second ([key in UnionTypeTwo]: boolean) is not allowed.

Upvotes: 1

Views: 93

Answers (1)

SLaks
SLaks

Reputation: 887275

Use intersection types:

export type MyTime = {
  [key in UnionTypeOne]?: {
    [name: string]: boolean
  }
} & {
  [key in UnionTypeTwo]: boolean
};

Upvotes: 3

Related Questions