Eu tenho um tipo TypeScript:
type SimpleUpdateCondition = {
[key: string]: SimpleUpdateCondition | [string, SimpleUpdateCondition] | string;
};
O que funciona bem, exceto que eu quero que cada nível de um objeto desse tipo tenha exatamente uma propriedade. Existe alguma maneira de restringir o número de chaves dinâmicas?
Encontrei uma pergunta semelhante , mas não consegui fazê-la funcionar com meu tipo recursivo.
Aqui está um exemplo do que quero dizer com TypeScript Playground
type SimpleUpdateCondition = {
[key: string]: SimpleUpdateCondition | [string, SimpleUpdateCondition] | string;
};
const helper = (updateCondition: SimpleUpdateCondition) => {
}
helper({one: { two: { three: 'leaf'}}}); // Valid
helper({}); // Should yield type error because it's empty
helper({one: { two: { three: 'leaf'}}, unwanted: 'yes'}); // Should yield type error because it have multiple properties at the root level
helper({one: {first: 'yes', second: 'no'}}) // Should yield type error because it has multiple properties at the second level, and so on...