Description
Search Terms
noImplicitAny, strict, any, unknown
Suggestion
When enabling "strict": true
or "noImplicitAny": true
in a project, could we assign unknown
to un-inferred/un-typed variables, instead of causing a compiler error? This would be a non-breaking change, since all cases where this would have an effect are already compiler errors.
Use Cases
It would allow avoiding having to specify a type when it's being type-checked anyway. The current implementation forces converted-from-js projects to deal with a lot of compiler errors from the outset (many of which use various runtime type-checking methods so should be happy with implicit unknown
s).
Examples
const getMessage = input => typeof input === 'string' ? input : 'hello';
const shortenMessage = message => message.substring(3);
the getMessage
example would be an error currently, but would be fine in the new world and the function would have return type string
.
the shortenMessage
example would have a changed error. instead of Parameter 'message' implicitly has an 'any' type
we'd see an error at message.substring
of Object is of type 'unknown'.
To me that's more intuitive in terms of what the problem actually is. message
is unknown so it should have type unknown
.
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. new expression-level syntax)