Question Details

No question body available.

Tags

typescript

Answers (1)

Accepted Answer Available
Accepted Answer
April 11, 2025 Score: 0 Rep: 26,166 Quality: High Completeness: 80%

You can assert that the type has only 1 prop with a factory function and a recursive generic type:

Playground

type SimpleUpdateCondition = {
  [k: string]: SimpleUpdateCondition | [string, SimpleUpdateCondition] | string
};

type UnionToIntersection = (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never

type IsUnion = [T] extends [UnionToIntersection] ? false : true

type OneProp = T extends unknown[] ? {[K in keyof T]: OneProp} : T extends object ? OnePropObject : T; type OnePropObject = keyof T extends never ? never : IsUnion extends true ? never : {[K in keyof T]: OneProp}

function makeUpdateCondition(obj: OneProp){ return obj; }

const c = makeUpdateCondition({ one: { id: ['string', {id: ''} ] } });

const c2 = makeUpdateCondition({}); // error

const c3 = makeUpdateCondition({ // error one: { id: '' }, two: { id: '' } });