-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Compiler option to disallow assignment of any to a more narrow type #11301
Comments
The motivation here looks very similar to #10715. |
This also works but shouldn't: const x: any = 'not a number';
const y: number = x; |
Is this just no-any ? |
No, no-any disallows any's altogether. I want to be able to use |
humm.. so what do you do with that any? it has to go somewhere? you never return it from a function, nor send it to another... so only |
You have to validate it. If it's function myPublicAddingFunction(x: any, y: any): number {
if (typeof x === 'number' and typeof y === 'number') {
// typescript now knows that x and y are numbers
return x + y;
} else {
throw 'numbers only';
}
} |
u can replace that with |
or just |
Replace what? And how does it fix my issue? |
replace |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
This currently compiles with no errors:
If
any
can only be assigned toany
, the compiler can assist in making sure untrusted data is properly type checked. So the example above would have to be changed toor
This is a simplified example, but reflects the frequent task of deserializing untrusted data to typed classes/interfaces where certain types are expected but should be checked.
More generally, it gives the developer confidence that a type really is a certain type in their code in a similar way that the non-nullable option does.
The text was updated successfully, but these errors were encountered: