-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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 codeThis wouldn't change the runtime behavior of existing JavaScript codeThis could be implemented without emitting different JS based on the types of the expressionsThis isn't a runtime feature (e.g. new expression-level syntax)To pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
Activity
RyanCavanaugh commentedon Oct 1, 2018
In principle this is true, but I suspect there's a lot of code like this where this change would introduce a giant wall of new errors:
We could potentially have some
implicitUnknown
flag instead thoughghost commentedon Oct 1, 2018
My 2 cents:
function f(x) {}
in the wild, I would assume thatnoImplicitAny
isn't set. I would be surprised to discover that the type of a variable changed due to a change in compiler options -- as far as I know no other programming language works that way. When you factor in conditional types this means that something that's astring
with one set of compiler options could be anumber
under different settings.string
s, not that I recommend that. It's more likely that the type annotation was forgotten by mistake, especially if the user mistakenly assumed that they had a contextual type.mmkal commentedon Oct 27, 2018
@RyanCavanaugh - good point, that case would break, so it probably would make sense to put this behind a flag if it were implemented.
@Andy-MS - isn't
strictNullChecks
a compiler option which can change types by being on?It's true that it's rare for a function to work for any input at all, but not that strange for it to be able to take many different forms, and do type checking (
typeof
/io-ts etc.) in the implementation.noImplicitAny
solves this problem but in a way that is both restrictive and potentially dangerous. Because the annotation is the only place the error appears, all other code using the un-annotated parameter compiles without problems, which makes it more likely that the code will make bad assumptions (e.g. not null-checking an optional property), and actually force the developer to eventually type it as any to make all the errors go away, which is a shame IMO.If
unknown
had been around since the beginning of typescript, I suspect this is how it'd work. There would be no compiler option(s) for it, un-annotated parameters would just beunknown
, because it's a fitting description!ExE-Boss commentedon Apr 3, 2019
@mmkal
Yes, yes it is (without it,
T|null
behaves likeT
, whereas with it, it actually behaves likeT | null
)Yeah, it is.
This is how Eclipse N4JS behaves.
ExE-Boss commentedon Apr 3, 2019
Also, it might be a good idea to have
implicitUnknown
overridenoImplicitAny
and have.d.ts
files withunknown
whereany
would have been generated before.unknown
type eclipse-n4js/n4js#1290implicitUnknown
complier flag #30813ExE-Boss commentedon Apr 14, 2019
I’ve done some prototyping work on this in #30813.
unknown
instead ofany
#31189no-unsafe-*
typescript-eslint/typescript-eslint#791nickserv commentedon Sep 11, 2019
I had a discussion recently where I realized it would be useful to replace any with unknown in situations like this, as it's a much safer value to work with and mirrors some of the recent changes made to type inference in generics.
However, a potential flaw in this suggestion is that if a type isn't checked in the same project, the author may forget to add a type that should be added explicitly when it instead returns unknown. For example, the user may be writing a library function that gets JSON from an API using
(await fetch(...)).json()
, which returnsPromise<any>
. This option would returnPromise<unknown>
instead of encouraging the user to explicitly add a type, which could cause type errors or unhelpful development experience from code using the library's return type in TypeScript.As a result, it would be safer to add an
implicitUnknown
option that changes implicit type generation to useunknown
instead ofany
(similar to what generic parameters already do) so that TypeScript users have the option of keeping noImplicitAny on. I think this could even be a good option to turn on by default, if it could be done in a way that didn't cause extra compatibility type errors.unknown
type annotation on catch clause variable #36775n9 commentedon Oct 26, 2020
Will this also handle arrays?
And what is the current best practice workaround to force
unknown[]
?mmkal commentedon Aug 10, 2021
I think it should, and in general it should work for generics:
@nickmccurdy I don't know if this rule existed in September 2019 when you wrote your comment, but no-unsafe-return could help with that scenario. But sure, for backwards-compatibility I guess the
noImplicitAny
flag would need to stay functional, even though it's kinda more of a linter responsibility. Three years on, I like the idea of havingimplicitUnknown
as a separate flag - it just-so-happens to makenoImplicitAny
a really easy requirement to fulfill.unknown
h3js/h3#387miguel-leon commentedon Dec 1, 2023
I would like to write:
it's shorter and easier to write.
But it errors with
noImplicitAny
. The suggestion in this thread could make that possible (?)... the compiler can't tell that the property types are not really being used and that the "implicit"any
is immediately discarded right?any
s tounknown
#60899