Consider this function for parsing strings.
const { a, b } = parseString({
string,
mapping: [{
param: 'a',
...
},{
param: 'b',
...
}]
Is there a way to restrict TypeScript to accept only 'a' and 'b' keys?
This is the current type definition I am using.
interface ParseStringArgs {
string: string;
mapping: {
start: string | null;
end: string | null
param: string;
}[];
}
I tried to define the key value for param.
export function parseString({ string, mapping }) {
...
type ParamKey = typeof mapping[number]['param'];
const result: Record<ParamKey, string> = {};
... some logic to populate result
return result
The output of parseString
currently is Record<string, string>
Can the return type be changed to Record<'a' | 'b', string>
?