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

Overloads returning type predicate types are always incompatible with implementation #4180

Closed
DanielRosenwasser opened this issue Aug 6, 2015 · 8 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@DanielRosenwasser
Copy link
Member

Version 1.6.0-dev.20150805

function f(x: string): x is string;
function f(x: number): x is number;
function f(x: any): boolean {
    return true;
}

Currently yields

blah.ts(1,10): error TS2394: Overload signature is not compatible with function implementa
tion.

I think this should be fixed in time for 1.6; being able to do this would enable some useful patterns.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Aug 6, 2015
@DanielRosenwasser
Copy link
Member Author

Specific use case found with @roganov here.

namespace ActionType {
    export type Foo = string & { _fooTag: any };
    export const Foo = <Foo>"Foo";

    export type Bar = string & { _barTag: any };
    export const Bar = <Bar>"Bar";
}

type ActionType = ActionType.Foo |
                  ActionType.Bar;


interface BaseAction {
    type: ActionType;
}

interface FooAction extends BaseAction {
    type: ActionType.Foo;
    foo: number;
}

interface BarAction extends BaseAction {
    type: ActionType.Bar;
    bar: string;
}

function isActionType(action: BaseAction, type: ActionType.Foo): action is FooAction;
function isActionType(action: BaseAction, type: ActionType.Bar): action is BarAction;
function isActionType(action: BaseAction, type: ActionType): boolean {
    return action.type === type;
}

let handleAction = (action: BaseAction) => {
    if (isActionType(action, ActionType.Foo)) {
        let foo = action.foo; // OK
        let bar = action.bar; // type error
    }
    else if (isActionType(action, ActionType.Bar)) {
        let bar = action.bar; // OK
    }
}

@tinganho
Copy link
Contributor

tinganho commented Aug 6, 2015

See #3262 (comment).

@DanielRosenwasser
Copy link
Member Author

Gotcha - but there was no formal issue filed for that, correct?

@tinganho
Copy link
Contributor

tinganho commented Aug 6, 2015

Correct, at least non that I knew of.

@mhegazy mhegazy added the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Aug 7, 2015
@DanielRosenwasser
Copy link
Member Author

@JsonFreeman in the comment that @tinganho linked to, you mentioned that this is a problem that "we've known about". What problem were you referring to specifically?

@JsonFreeman
Copy link
Contributor

I was referring to #943.

@DanielRosenwasser
Copy link
Member Author

Right, I was sure we had an issue referring to that. Glad I'm not going nuts 😄. @mhegazy.

@DanielRosenwasser
Copy link
Member Author

This should be fixed for TypeScript 1.8.

@DanielRosenwasser DanielRosenwasser added the Fixed A PR has been merged for this issue label Dec 18, 2015
@DanielRosenwasser DanielRosenwasser removed the Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. label Dec 18, 2015
@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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants