-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
replace implicit 'any' with 'unknown' #27265
Comments
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 |
My 2 cents:
type F<T> = string extends (T extends {} ? string : boolean) ? string : number;
// Currently returns string; if `x: unknown`, returns number
function f(x): F<typeof x> {
return <any>null;
}
|
@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 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 ( If |
Yes, yes it is (without it,
Yeah, it is.
This is how Eclipse N4JS behaves. |
Also, it might be a good idea to have |
I’ve done some prototyping work on this in #30813. |
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 As a result, it would be safer to add an |
Will this also handle arrays? function foo(input: {}) {
if (input instanceof Array) {
// `input` is currently `any[]` instead of `unknown[]`
if (input.length > 0) {
console.log(input[0].anyProp);
}
} else if (...) {
...
}
} And what is the current best practice workaround to force |
I think it should, and in general it should work for generics: class MyClass<T> {
constructor(readonly value: T) {}
}
function foo(input: {}) {
if (input instanceof MyClass) {
// input is MyClass<any> now, should be MyClass<unknown>
console.log(input.value.thisPropertyProbablyDoesNotExist())
}
} @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 |
I would like to write: type x = keyof { a, b, c, d, e, f };
// instead of
type y = 'a' | 'b' | 'c' | 'd' | 'e' | 'f'; it's shorter and easier to write. But it errors with |
Search Terms
noImplicitAny, strict, any, unknown
Suggestion
When enabling
"strict": true
or"noImplicitAny": true
in a project, could we assignunknown
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
the
getMessage
example would be an error currently, but would be fine in the new world and the function would have return typestring
.the
shortenMessage
example would have a changed error. instead ofParameter 'message' implicitly has an 'any' type
we'd see an error atmessage.substring
ofObject 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 typeunknown
.Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: