Skip to content

typecasting to a generic interface from an object literal #13931

@sramam

Description

@sramam

TypeScript Version: 2.1.5

interface Orig<T extends string> {
    type: T;
}

type TypeId = 'SpecA';

interface SpecA extends Orig<TypeId> {
    name?: string;
}

const goodCast = <SpecA>{
    type: 'SpecA'
}

const badCast = <SpecA>{
    type: 'SpecB'
}
// throws an error:
// Type '{ type: "SpecB"; }' cannot be converted to type 'SpecA'.
//  Types of property 'type' are incompatible.
//    Type '"SpecB"' is not comparable to type '"SpecA"'.

const justData = {
    type: 'SpecB'
};

const castLater = <SpecA>justData;
// Expected an error to be thrown, but the compiler accepts this.

Expected behavior:
The typecast to castLater should trigger a compile error.

Actual behavior:
it doesn't.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions