Answer the question
In order to leave comments, you need to log in
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;
}
Answer the question
In order to leave comments, you need to log in
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
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question