I
I
IdoNotKnowWhatToSay2021-06-15 23:39:38
typescript
IdoNotKnowWhatToSay, 2021-06-15 23:39:38

How can I make interface or type properties start with a specific character?

Tried to solve it with template literals, but didn't work.

interface SomeInterface {
    '@prop1': string;
    '@prop2': string;
    '@propN': string;
}


Is there a way to avoid having to add properties manually? Any property must start with the "@" sign. There can be as many properties as you like!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WbICHA, 2021-06-16
@IdoNotKnowWhatToSay

Unfortunately, literals with keys don't work, so just like this:

const asSomeInterface = <K extends PropertyKey>(obj: { [P in K]: P extends `@${string}` ? string : never }) => obj;

asSomeInterface({
  '@asd': 'asd',
  'asd': 'asd', // error
});

In any case, for now, as I understand it, it is likely that this will be fixed in the foreseeable future.
https://stackoverflow.com/questions/65878880/types...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question