2

Given an interface of known keys and values, is it possible to tell TypeScript that I want to have a type that has all the same keys of my original interface with value types extended via union of the original type and another type. Pseudo (sadly non-functional) TypeScript code that describes the desired behaviour:

type FancyCSSObject = { [key: keyof CSSObject]: CSSObject[key] | OtherType };

The above is not valid TypeScript, so my question is, how to express this in valid TypeScript code?

1 Answer 1

2

Before I finished posting this, I realised the solution to this problem, however seeing that it's sort of a corner-case that's not documented in its entirety, I will share my solution, in case someone else needs this:

type FancyCSSObject = {
  [key in keyof CSSObject]: CSSObject[key] | OtherType;
};

I was close to the solution. I hope this helps someone.

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

Comments

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.