-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrectly inferred type for union types #43962
Comments
I'm not sure if this is a bug or not; a much shorter repro would be appreciated. |
@RyanCavanaugh I removed some non-required declaration but even shorter is unreal 😊 Part of declarations is a mock of rxjs-library (like type Op<I, O> = (thing: Thing<I>) => Thing<O>;
type Thing<T> = {
value: T;
pipe<A, B, C>(
opA: Op<T, A>,
opB: Op<A, B>,
): Thing<B>;
};
type Wrapped<V> = { data: V }
type WrappedInferredData<T> = T extends Wrapped<infer R> ? R : T;
declare function createThing<T>(result: T): Thing<T>;
declare function map<T, R>(project: (value: T) => R): Op<T, R>;
declare function tap<T>(next: (value: T) => void): Op<T, T>;
declare function addSettings<V>(data: V | Wrapped<V>): Wrapped<WrappedInferredData<V | Wrapped<V>>>;
declare function functionWithTypingIssue<V>(factory: () => Thing<V | Wrapped<V>>): Thing<V>;
const thisIsTargetValue = 1;
const result = functionWithTypingIssue(() => {
const thing = createThing(thisIsTargetValue);
return thing.pipe(
map((data) => addSettings(data)),
tap(v => v)
);
});
result.value.data; // should be of type thisIsTargetValue |
Thanks for providing the versions, I was able to bisect this to #31662. |
Just to give the test case I'm actually testing here: Playground Link (It's somewhat smaller but not much) |
My understanding so far is that when we are inferring and we detect that the two types have the same alias symbol, we will just infer from the type arguments and return early. #31662 changed things in a way that some inferences we made before become contravariant inferences instead. Then, we pick If I instead infer from type arguments, but don't return early and allow it to continue, the bug goes away. It seems like doing this equal symbol short circuit is not always correct because it gives a different result than a normal inference. Not returning also gets rid of a couple very silly looking error elaborations, but then we end up regressing #42320 as we determined that circularity to be okay (and this symbol short circuit is what makes it legal). There are other bugs I've seen in #31662 while working on this issue, but I don't have any repros that actually change. Namely, |
Bug Report
🔎 Search Terms
infer, never, union types, error TS2345
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
Type incorrectly infers from union type
🙂 Expected behavior
Type should be resolved correctly.
typeof thisIsTargetValue === number
for this exampleThe text was updated successfully, but these errors were encountered: