Is there a recommended way to create a Record using a type as a key?

None of these seem to work:
const stringKeys = type(
"===",
"CAR",
"ENDO",
"REP",
"DEV",
"GEN",
"NEU",
"ORG",
"IMM",
"RES",
"IRR",
"ECO",
);

const explicit = type.Record(stringKeys, type("number | null"));

const stringSyntax = type(
'Record<"CAR" | "ENDO" | "REP" | "DEV" | "GEN" | "NEU" | "ORG" | "IMM" | "RES" | "IRR" | "ECO", number | null>',
);
const stringKeys = type(
"===",
"CAR",
"ENDO",
"REP",
"DEV",
"GEN",
"NEU",
"ORG",
"IMM",
"RES",
"IRR",
"ECO",
);

const explicit = type.Record(stringKeys, type("number | null"));

const stringSyntax = type(
'Record<"CAR" | "ENDO" | "REP" | "DEV" | "GEN" | "NEU" | "ORG" | "IMM" | "RES" | "IRR" | "ECO", number | null>',
);
2 Replies
francis
francisOP4w ago
I get the error ParseError: Index keys "CAR", "DEV", "ECO", "ENDO", "GEN", "IMM", "IRR", "NEU", "ORG", "REP", "RES" should be specified as named props.
ssalbdivad
ssalbdivad4w ago
Yeah unfortunately this was a mistake in how the parser was implemented to ensure named properties are normalized in their representation: https://github.com/arktypeio/arktype/issues/952 The easiest thing to do for now would probably just be to define that initial set of keys as a tuple, then you can map it with Object.fromEntries to build the type you want.
GitHub
Allow index node to be parsed from enumerable keys · Issue #952 · a...
Currently, an index signature including a literal key like "a" is an error due to some limitations of how the parser is implemented. There is a normalizeIndexKey function, but it is not u...
Want results from more Discord servers?
Add your server