Skip to content

Missing type-check on explicit cast? #9312

Closed
@mindplay-dk

Description

@mindplay-dk

TypeScript Version:

1.8.10

Code

interface Foo {
    foo()
    bar()
}

// does NOT error:

var a = <Foo> {
    foo() {}
    // missing bar() - no error!
}

var b = {
    foo() {}
    // missing bar() - no error!
} as Foo

// errors: (b not assignable to type Foo)

var c: Foo = {
    foo() {}
    // missing bar() - error
}

function d() : Foo {
    return {
        foo() {}
        // missing bar() - error
    }
}

In the two first cases, the shape (and inferred type) of the anonymous object are missing a method and clearly are not assignable to Foo - I was expecting an error message.

I don't fully understand why assignment to a var vs assignment to an explicitly typed var or return-type would behave inconsistently in terms of type-checking.

Didn't it use to be that you had to do something like var a = <Foo> <any> { .. }, e.g. you had to explicitly cast the inferred object type/shape to any before you'd be allowed to do an unsafe type-cast?

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