0

I have the following type:

type MyKeys = 'foo' | 'bar' | 'baz'

I want to define a type that has keys of type MyKeys, but also extends it with more keys, like this:

type FooType = {
  [key in MyKeys]: boolean
  quux: boolean   // <--- Error: '}' expected.ts(1005)
}

How can I use both generic keys and explicit key names?

1
  • I'm also not sure if "generic keys" is what you call this, nor if "explicit keys" makes sense Commented Mar 7, 2019 at 16:34

1 Answer 1

2

You can use an Intersection Type:

type FooType = {[key in MyKeys]: boolean} & {
  quux: boolean
}

or as mentioned by @jcalz, you can also use Record<>:

Record<MyKeys | 'quux', boolean>

that will equivalent to:

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

That's an interesction, not a union.
You can also do Record<MyKeys | 'quux', boolean>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.