Skip to content

add an option that flags coercion #7746

Closed
@zpdDG4gta8XKpMCd

Description

@zpdDG4gta8XKpMCd

There is a category of bugs caused by unintended coercion at runtime. Such bugs can be found by the compiler. Having an option that bans such coercion would be a great addition.

examples of the problem:

// before (typechecks)
let isFirst: boolean;
// ... later in code
if (!isFirst) {
   // do stuff
}

// after refactoring (still typechecks)
let isFirst: () => boolean;
// ... later in code
if (!isFirst) {
  // got a bug, unreachable code
}
// before (typechecks)
let counter: number;
let kind: string;
// ... later in code
let id = '--' + kind + (counter++); // works: --blah-blah-341279

// after refactoring (still typechecks)
let counter: number;
let kind: {
    type: string;
    sort: string;
};
// ... later in code
let id = '--' + kind + (counter++); // now a bug: --[object Object]-341279

Metadata

Metadata

Assignees

No one assigned

    Labels

    SuggestionAn idea for TypeScriptToo ComplexAn issue which adding support for may be too complex for the value it adds

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions