Skip to content
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

Partial type inference failures with tagged union types #13754

Closed
falsandtru opened this issue Jan 30, 2017 · 6 comments
Closed

Partial type inference failures with tagged union types #13754

falsandtru opened this issue Jan 30, 2017 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@falsandtru
Copy link
Contributor

Probably an example without the type guard of #13454.

TypeScript Version: master

Code

declare function f<P, R, S>(process: Supervisor.Process<P, R, S> | Supervisor.Process.Call<P, R, S>): void;
namespace Supervisor {
  export type Process<P, R, S> = {
    readonly init: Process.Init<S>;
    readonly call: Process.Call<P, R, S>;
    readonly exit: Process.Exit<S>;
  };
  export namespace Process {
    export type Init<S> = (state: S) => S;
    export type Call<P, R, S> = (param: P, state: S) => [R, S] | PromiseLike<[R, S]>;
    export type Exit<S> = (state: S, reason: any) => void;
  }
}
f<void, void, void>({
  init: s => s,
  call: (p, s) => <[void, void]>[p, s],
  exit: s => void s,
});

Expected behavior:

ok

Actual behavior:

$ node built/local/tsc.js -t es6 index.ts --noImplicitAny
index.ts(16,10): error TS7006: Parameter 'p' implicitly has an 'any' type.
index.ts(16,13): error TS7006: Parameter 's' implicitly has an 'any' type.
@HerringtonDarkholme
Copy link
Contributor

HerringtonDarkholme commented Jan 30, 2017

It seems the problem comes from call in Process clashes with call in Function.

Smaller reproduction.

declare function f(process: A | B): void;

type A = {
  call(p: number): void
  brand: number
}
type B = {
  call(p: string): void
  brand: string
}

f({
  brand: 123,
  call(n) {} // any
})

To my best knowledge, compiler will not narrow union type in argument inference, for now.

A better alternative is using overloading.

declare function f<P, R, S>(process: Supervisor.Process<P, R, S>): void;
declare function f<P, R, S>(process: Supervisor.Process.Call<P, R, S>): void;

@falsandtru
Copy link
Contributor Author

Oh it is a nice workaround! Thanks!

@falsandtru falsandtru changed the title Inference failures with tagged union types Partial inference failures with tagged union types Jan 30, 2017
@falsandtru falsandtru changed the title Partial inference failures with tagged union types Partial type inference failures with tagged union types Jan 31, 2017
@falsandtru
Copy link
Contributor Author

I found a problem in this workaround. The overload should be:

declare function f<P, R, S>(process: Supervisor.Process.Call<P, R, S>): void;
declare function f<P, R, S>(process: Supervisor.Process<P, R, S>): void;

details: #13780

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 24, 2017
@mhegazy
Copy link
Contributor

mhegazy commented May 25, 2017

looks like a duplicate of #13780

@mhegazy mhegazy added Duplicate An existing issue was already created and removed Needs Investigation This issue needs a team member to investigate its status. labels May 25, 2017
@mhegazy
Copy link
Contributor

mhegazy commented May 25, 2017

The contextual type for the function is the union type. the union type property does not have a call signature because they are not identical. see #7294 for more details.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 9, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Jun 9, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants