-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Open
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolution
Milestone
Description
Bug Report
π Search Terms
union, type inference, imprecise union generic type, generic type.
π Version & Regression Information
This is reproducible on all versions of the playground.
β― Playground Link
Playground link with relevant code
π» Code
export interface Something<
R extends
| {
[K in 'foo' | 'bar' | 'baz']?: {
r?: unknown;
p?: unknown;
};
}
| undefined = undefined
> {}
const o: Something<{ foo: { r: 1; p: 2 }; bar: { r: 3 } }> = {};
declare function something<M extends T extends Something<infer R> ? keyof R : never, T extends Something>(
key: M,
something: T
): M;
declare function somethingBugged<M extends T extends Something<infer R> ? keyof R : never, T extends Something>(
key: M,
something: T
): [M];
const normal = something('foo', o);
// got: "foo"
// expected: "foo"
const bugged = somethingBugged('foo', o);
// got: ["foo" | "bar"]
// expected: ["foo"]
// However with `as const`, the correct type is returned.
const ugly = somethingBugged('foo' as const, o);
// got: ["foo"]
// expected: ["foo"]π Actual behavior
The type loses its precision and is the union instead of the exact type.
π Expected behavior
The type is exact.
What is strange is that the generic type M loses precision ("foo" -> "foo" | "bar") with the somethingBugged function which returns a tuple of M (and it does the same thing with any other "wrapper", such as an object).
Whereas with the something function there is no problem. Both functions are identical.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolution