Skip to content

[Feature Request] Flag for disabling implicit coerisions #39209

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

Closed
4 of 5 tasks
redradist opened this issue Jun 23, 2020 · 6 comments
Closed
4 of 5 tasks

[Feature Request] Flag for disabling implicit coerisions #39209

redradist opened this issue Jun 23, 2020 · 6 comments
Labels
Duplicate An existing issue was already created

Comments

@redradist
Copy link

redradist commented Jun 23, 2020

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.
@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 23, 2020
@RyanCavanaugh
Copy link
Member

We're not going to add a flag that causes errors on any, so that's right out. A flag for disabling boolean coercion is discussed at #14114

@redradist
Copy link
Author

redradist commented Jun 23, 2020

@RyanCavanaugh

We're not going to add a flag that causes errors on any, so that's right out. A flag for disabling boolean coercion is discussed at #14114

It is not duplicate

I am not taking about boolean coercion, but about coercion at all

I want to enforce users to write by default safe code, that's all

It is possible by enforcing users to use only explicit coercions, but not implicit

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

or

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

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@redradist
Copy link
Author

@RyanCavanaugh Have you seen my comment ? Will you remove tag Duplicate ?

@redradist
Copy link
Author

@ahejlsberg I just want to call to you as Lead of the language ...

Don't you think this Feature Request should be implemented ? This very useful that make TypeScript even more safe ...

@RyanCavanaugh
Copy link
Member

@redradist We're tracking this adequately; thank you for the feedback. See also #38905

@microsoft microsoft locked as resolved and limited conversation to collaborators Jun 26, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants