Skip to content

union type does not work in classes/interfaces #23031

@babkenmes

Description

@babkenmes

TypeScript Version: 2.4.2

Have tryed in http://www.typescriptlang.org/play the error is the same

Code

class Action { 
    type: string;
    payload: number;
}

type ActionCreator = ((page:number)=>Action) | Action

class Filter {
    actionCreator: ActionCreator;
    subStore: string;
}
const x: ActionCreator = (page: number) => {
    return {
        type: "foo",
        payload: 1
    }
}
const y: Filter = {
    actionCreator: (page: number) => { return {
        type: "foo",
        payload: 1
    } },
    subStore:"foo"
}

y.actionCreator(5);
x(5);

For y.actionCreator(5) I get

Cannot invoke an expression whose type lacks a call signature. Type 'ActionCreator' has no compatible call signatures.

Expected behavior: / Actual behavior:
As you can see it works for x, it should work the same way for the class/interface implementation.

Playground Link

Related Issues:

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions