Skip to content

Incorrectly inferred type for union types #43962

Closed
@RomanRolginSky

Description

@RomanRolginSky

Bug Report

🔎 Search Terms

infer, never, union types, error TS2345

🕗 Version & Regression Information

  • This changed between versions 3.5.1 and all newer versions up to 4.3.0-beta. 3.5.1 doesn't throw such error, there is the error in 3.6.2

⏯ Playground Link

Playground link with relevant code

💻 Code

type Thing<T> = {
    value: T;
    pipe<A, B, C>(
        opA: Op<T, A>,
        opB: Op<A, B>,
        opC: Op<B, C>
    ): Thing<C>;
};
type Op<I, O> = (thing: Thing<I>) => Thing<O>;

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>;

interface Wrapped<V> {
    data: V;
    props?: object;
}
declare function isWrapped<T>(obj: any): obj is Wrapped<T>;
declare function unwrap<T>(obj: T | Wrapped<T>): T;

type WrappedInferredData<T> = T extends Wrapped<infer R> ? R : T;
type WrappedInput<V> = () => Thing<V>;

declare function addSettings<V>(
    data: V | Wrapped<V>,
    props: object
): Wrapped<WrappedInferredData<V | Wrapped<V>>>;

const functionWithTypingIssue = <V>(factory: WrappedInput<V | Wrapped<V>>): Thing<V> => {
    return factory().pipe(map((item) => unwrap(item)), map(item => item), map(item => unwrap(item)));
}

const thisIsTargetValue = 1;

const a$ = functionWithTypingIssue(() => {
    const thing = createThing(thisIsTargetValue);
    return thing.pipe(
        map((data) => addSettings(data, { anyProp: 1 })),
        map((data) => data), // error here
        tap((v) => v)
    );
});

a$.value; // should be of type thisIsTargetValue
Argument of type 'Op<Wrapped<number>, Wrapped<number>>' is not assignable to parameter of type 'Op<Wrapped<number>, Wrapped<never>>'.
  Type 'Wrapped<number>' is not assignable to type 'Wrapped<never>'.
    Type 'number' is not assignable to type 'never'. 

🙁 Actual behavior

Type incorrectly infers from union type

🙂 Expected behavior

Type should be resolved correctly. typeof thisIsTargetValue === number for this example

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions