io-ts formally recommends to use keyof
for better performance with string literal unions. Thankfully, that also makes this problem much easier to solve:
export const CONTROLS = [
"section",
"text",
"richtext",
"number",
] as const;
function keyObject<T extends readonly string[]>(arr: T): { [K in T[number]]: null } {
return Object.fromEntries(arr.map(v => [v, null])) as any
}
const ControlType = t.keyof(keyObject(CONTROLS))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…