Skip to content

Narrowing down type bypasses type checker #30615

@bpasero

Description

@bpasero

TypeScript 3.3.1

This imho should not be allowed:

const foo = { foo: false } as { foo: boolean, bar: boolean };

As it bypasses the type checker from doing its job. I now have basically introduced a type with undefined as foo.bar even though it should be boolean | undefined.

If someone wants this type by all means, it is still possible by using any.

const foo = { foo: false } as any as { foo: boolean, bar: boolean };

That way at least one can find hacks easily.

I would like to see a warning for this, possibly via some kind of new strict rule ideally.

I actually use as or <> a lot in my code to get IntelliSense for an object literal I am writing and ensuring the values I give it are correct, but now I am not so sure anymore.

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