Repository navigation
New --strictAny mode - #24423
New --strictAny mode#24423Anders Hejlsberg (ahejlsberg) wants to merge 8 commits into
Conversation
# Conflicts: # src/compiler/checker.ts
|
Anders Hejlsberg (@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
--strictAnycompiler option in the--strictfamily of options. This mode forbids unsafe conversions fromanyto other types. Specifically, in--strictAnymode,anyis assignable only toanyand an empty object type ({}). The--strictAnyoption doesn't otherwise alter the special behaviors ofany, i.e. unrestricted property access and function calls through objects typed asanycontinue to be permitted.The
--strictAnyoption effectively identifies transitions from the loosly typed world ofanyto 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
--strictAnymode requires such unsafe conversions to be accompanied by dynamic checks or explicit type assertions:The
--strictAnyoption 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
--strictAnyis potentially quite disruptive to code bases that useanyliberally. However, in code bases that are intended to be strongly typed throughout it definitely can be both revealing and informative. For example,--strictAnyidentifies 100+ locations in the TypeScript compiler where we have unsafe conversions fromany, many of which are unintended.