镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

add an option that flags coercion #7746

Description

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

Activity

  1. RyanCavanaugh commented on Mar 31, 2016

    @RyanCavanaugh
    Member

    Your first example is identical to a suggestion you've already logged.

    Your second example doesn't typecheck.

  2. zpdDG4gta8XKpMCd commented on Mar 31, 2016

    @zpdDG4gta8XKpMCd
    Author
    1. technically it was about a more specific case (and probably because of that it got closed), which alone didn't make much sense, this is a broader request that might have more sense
    2. fixed
  3. zpdDG4gta8XKpMCd commented on Apr 2, 2016

    @zpdDG4gta8XKpMCd
    Author

    another example out of the oven

    // before
    declare function toBypassReasons(): string[];
    let message = toBypassReasons().join(', ');
    
    // after refactoring
    interface BypassReason { reason: string; ruleKey: string; }
    declare function toBypassReasons() : BypassReason[];
    let message = toBypassReasons().join(', '); // <-- bug
  4. RyanCavanaugh commented on Apr 2, 2016

    @RyanCavanaugh
    Member

    There is literally zero coercion in that example. You'd need completely different mechanics (somehow disallowing certain methods when a type parameter is one value vs another) to detect that "bug" vs the other two examples.

  5. mhegazy commented on Jun 7, 2016

    @mhegazy
    Contributor

    closing in favor of #7989

  6. added
    Too ComplexAn issue which adding support for may be too complex for the value it adds
    and removed
    Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.
    on Jun 8, 2016
  7. RyanCavanaugh commented on Jun 9, 2016

    @RyanCavanaugh
    Member

    Tracking a new issue at #9041 that solves some of these cases

  8. locked and limited conversation to collaborators on Jun 19, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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