Skip to content

Cannot cast union type to an implemented interface if the class contains more properties #6960

@steinso

Description

@steinso
interface IFoo {
    foo: string;
}

interface IBar {
    bar: string;
}

class Foo implements IFoo{
    foo: string;
}

class Bar implements IBar{
    bar: string;
    newProperty: string;
}

let x:Foo|Bar = new Bar();
let y:IFoo =  x as IFoo; // Works as expected, no errors

let u:Foo|Bar = new Bar();
let z:IBar = u as IBar; //Does not work IFF Bar contains more properties than IBar

We have a situation where a property is a union of several classes, and we would like to cast it to the correct interface in different situations. However this does not seem to work if the class has properties that is not present on the implemented interface. Playground link

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions