-
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
New --strictAny mode #24423
New --strictAny mode #24423
Conversation
# Conflicts: # src/compiler/checker.ts
@ahejlsberg if I understand correctly, with If property acceses and function calls to Can you comment on why |
That is technically true, but I'm not sure it would ever be practical. I think the issue of introducing an
I think the core intuition people have with the |
I see your point that ideally we'd want both
Yes I suppose that's true usually. One case that's still a problem is when the
EDIT: PR for |
Is it similar to TSLint rule? |
I've got to admit that after thinking about this PR, I do have some reservations. My feeling is that this behavior is what I want from declare function foo(x: Some & Very & Long<Type, Annotation | Have<Fun, Writing, This>>);
// Now an error!
foo({ /*...*/ } as any); The above call to The workaround is actually very simple - use declare function foo(x: Some & Very & Long<Type, Annotation | Have<Fun, Writing, This>>);
// Always worked!
foo({ /*...*/ } as never); But now you really have to know how both of these work, whereas I suspect most users have never been aware of As a side note, I think it's kind of cute that |
Shouldn't this be |
After a lot of discussion on the current implementation, we've decided that |
This PR implements a new
--strictAny
compiler option in the--strict
family of options. This mode forbids unsafe conversions fromany
to other types. Specifically, in--strictAny
mode,any
is assignable only toany
and an empty object type ({}
). The--strictAny
option doesn't otherwise alter the special behaviors ofany
, i.e. unrestricted property access and function calls through objects typed asany
continue to be permitted.The
--strictAny
option effectively identifies transitions from the loosly typed world ofany
to the strongly typed world of other types and requires such transitions to be accompanied by proof (through control flow based type narrowing) or assumption of responsibility (through explicit type assertions).An example:
The
--strictAny
mode requires such unsafe conversions to be accompanied by dynamic checks or explicit type assertions:The
--strictAny
option includes an exception for assignability of function types, permitting strongly typed parameters in the source to be matched by a rest parameter of typeany[]
in the target even though that is technically not safe. For example:We could potentially remove this exception and instead require such general function types to use type
never
, which would ensure that it isn't possible to call the function without first explicitly asserting a more specific type:Note that
--strictAny
is potentially quite disruptive to code bases that useany
liberally. However, in code bases that are intended to be strongly typed throughout it definitely can be both revealing and informative. For example,--strictAny
identifies 100+ locations in the TypeScript compiler where we have unsafe conversions fromany
, many of which are unintended.