Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an option that flags coercion #7746

Closed
zpdDG4gta8XKpMCd opened this issue Mar 31, 2016 · 6 comments
Closed

add an option that flags coercion #7746

zpdDG4gta8XKpMCd opened this issue Mar 31, 2016 · 6 comments
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds

Comments

@zpdDG4gta8XKpMCd
Copy link

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
@RyanCavanaugh
Copy link
Member

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

Your second example doesn't typecheck.

@zpdDG4gta8XKpMCd
Copy link
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

@DanielRosenwasser DanielRosenwasser added Suggestion An idea for TypeScript Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. labels Apr 1, 2016
@zpdDG4gta8XKpMCd
Copy link
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

@RyanCavanaugh
Copy link
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.

@mhegazy
Copy link
Contributor

mhegazy commented Jun 7, 2016

closing in favor of #7989

@mhegazy mhegazy closed this as completed Jun 7, 2016
@RyanCavanaugh RyanCavanaugh added Help Wanted You can do this Too Complex An issue which adding support for may be too complex for the value it adds and removed Needs Proposal This issue needs a plan that clarifies the finer details of how it could be implemented. Help Wanted You can do this labels Jun 8, 2016
@RyanCavanaugh
Copy link
Member

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

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Suggestion An idea for TypeScript Too Complex An issue which adding support for may be too complex for the value it adds
Projects
None yet
Development

No branches or pull requests

4 participants