You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Conditional types do not work in generic functions #53455
typeUndefinedKeys<T>={[PinkeyofT]: undefinedextendsT[P] ? P : never}[keyofT]typeOptionalUndefined<T>=Partial<Pick<T,UndefinedKeys<T>>>&Omit<T,UndefinedKeys<T>>typeTSample<UextendsRecord<string,any>|undefined,DextendsRecord<string,any>>={undef: U,def: D}functionfn<UextendsRecord<string,any>|undefined,DextendsRecord<string,any>>(params: OptionalUndefined<TSample<U,D>>){{// ERROR: both properties do not exist on type 'OptionalUndefined<TSample<U, D>>'const{ def, undef }=params}{// works as expectedconst{ def, undef }=paramsasOptionalUndefined<TSample<Record<string,any>|undefined,Record<string,any>>>}}
🙁 Actual behavior
typescript cannot properly determine parameter type
🙂 Expected behavior
I expect typescript to use definition of U (extends Record<string, any> | undefined) and D (``) to be used to determine the type of parameters and params should have `{ undef?: Record<string, any> | undefined, def: Record<string, any> }` type?
Notice how, when I explicitly specify the type of params, it works properly.
Activity
MartinJohns commentedon Mar 23, 2023
Resolving of conditional types including unbound type arguments is deferred. This is a design limitation.
TypeScript can't resolve your conditional type within the function because it doesn't know what types
UandDare.fatcerberus commentedon Mar 23, 2023
Conveniently, this issue makes a good case against #53452.
Partialby itself currently works in generic contexts.