Skip to content

[Feature Request] Flag for disabling implicit coerisions #39209

Closed
@redradist

Description

@redradist

Search Terms

safe typescript, predictable behavior, no implicit coercion

Suggestion

I would like to suggest to introduce a new flag for disabling implicit type coercion at all
Something like this:

--predicatableJs

Or some other flag

Use Cases

To make Typescript even more safe

I want to enforce my team to use only predictable features and if they want old style behavior, just to write another module or disable warning at the top of the file (in such way all reviewers of this files will see that)

Examples

Instead of writing either:

let strange_boolean: any = true;
let strange_string: string = "1";
console.log(strange_boolean == strange_string);

or

let strange_number: any = 2;
let strange_string: string = "1";
console.log(strange_number == strange_string);

I want to enforce that this code produce error and compiles only if there is explicit cast:

let strange_boolean: any = true;
let strange_string: string = "1";
console.log(String(strange_boolean) == strange_string);
let strange_boolean: any = true;
let strange_string: string = "1";
console.log(Boolean(strange_boolean) == Boolean(strange_string));
let strange_number: any = 2;
let strange_string: string = "1";
console.log(String(strange_number) == strange_string);
let strange_number: any = 2;
let strange_string: string = "1";
console.log(strange_number == Number(strange_string));

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions