1

Is it possible to do some rudimentary string manipulations, as you're converting literal unions to type names?

Example:

type Strings = "Fork" | "Spoon";

type GetMethods = { // I want this to be automatically generated basing on Strings
   getFork: Function,
   getSpoon: Function
}

1 Answer 1

2

In TypeScript v.4.1, (which looks like it will be released in less than 2 weeks time) you can use Template Literal Types to do this really easily:

type Strings = "Fork" | "Spoon";

type FuncNames = `get${Strings}`;

type GetMethods = { [K in FuncNames]: Function }

Playground link

Prior to TS4.1, it doesn't look like this is possible.

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.