Skip to content

Allow type assertions from T-constrained type variable to subtypes of T #20821

@ghoullier

Description

@ghoullier

TypeScript Version: 2.6.2

Code

enum ActionType {
    Call
}

interface Action {
    type: ActionType;
}

interface CallAction extends Action {
    context: any;
}

class Service {
    exec<T extends Action>(action: T) {
        if (action.type == ActionType.Call) {
            (<CallAction>action).context = {};
        }
    }
}

Expected behavior:

I should use cast because CallAction extends Action

Actual behavior:

I have the following compilation error:

(<CallAction>action).context = {};
Type 'T' cannot be converted to type 'CallAction'.
Type 'T' cannot be converted to type 'CallAction'.
  Type 'Action' is not comparable to type 'CallAction'.
    Property 'context' is missing in type 'Action'.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions