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
With a particular type-setup, infer does not provide the correct type. When you open the playground link, you will see the type Opts and the types ExtractReq and ExtractOpt. Opts takes two type parameters, Required and Optional and ExtractReq and ExtractOpt will extract these two types using infer. However, the extracted types are, for the most part, incorrect.
🔎 Search Terms
infer, generic
🕗 Version & Regression Information
After updating TS to version 4.2.*. I have tried the nightly version and the problem did still arise.
// Opts type:typeOpts<RequiredextendsRecord<string,unknown>|Never=Never,OptionalextendsRecord<string,unknown>|Never=Never>=Required&Partial<Optional>;// extract typestypeExtractReq<T>=TextendsOpts<infer Required,any> ? Required : never;typeExtractOpt<T>=TextendsOpts<any, infer Optional> ? Optional : never;// shortand for Opts type with specific type parameterstypeOpts3=Opts<{a: string},{b: string}>;// this type is Partial<{ a: string }> & Partial<{ b: string }> although { a: string } was expected.typeReq3=ExtractReq<Opts3>;// this type is Never | Record<string, unknown> although { b: string } was expected.typeOpt3=ExtractOpt<Opts3>;
🙁 Actual behavior
The types that are inferred by ExtractReq and ExtractOpt are incorrect (see code above / playground link).
🙂 Expected behavior
The type { a: string } in the case of Req3 and { b: string } in the case of Opt3.
The text was updated successfully, but these errors were encountered:
Once you've mixed two types together into a new type, you can't "unmix" them this way unless some very particular conditions (i.e. ones you shouldn't rely on) are met. Our type alias preservation changes in 4.2 changed this example from a "happens to work" to a "happens to not work".
@RyanCavanaugh is there any other, more proper, way to "unmix" the types of a composed type as mentioned by OP.
E.g., from a type that is a Opts<T, Q>, obtain the particular T and Q.
I also had (important) code relying on this feature, and it stopped working after updating TS to 4.2.
Bug Report
With a particular type-setup,
infer
does not provide the correct type. When you open the playground link, you will see the typeOpts
and the typesExtractReq
andExtractOpt
.Opts
takes two type parameters,Required
andOptional
andExtractReq
andExtractOpt
will extract these two types usinginfer
. However, the extracted types are, for the most part, incorrect.🔎 Search Terms
infer, generic
🕗 Version & Regression Information
After updating TS to version 4.2.*. I have tried the nightly version and the problem did still arise.
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The types that are inferred by
ExtractReq
andExtractOpt
are incorrect (see code above / playground link).🙂 Expected behavior
The type
{ a: string }
in the case ofReq3
and{ b: string }
in the case ofOpt3
.The text was updated successfully, but these errors were encountered: