-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Add a flag which priorities unknown over any in any | unknown
#46347
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
Comments
I just though of an alternative solution which turns out to be really close to #45605 Add a compiler option to forcefully turn For example, given: a.d.ts: export declare const unknownA: unknown;
export declare const anyA: any; b.ts: export declare const unknownB: unknown;
export declare const anyB: any; This would be the behaviour: c.ts ( import { unknownA, anyA } from './a';
import { unknownB, anyB } from './b';
// $ExpectType unknown
unknownA;
// $ExpectType any
anyA;
// $ExpectType unknown
unknownB;
// $ExpectType any
anyB; c.ts ( import { unknownA, anyA } from './a';
import { unknownB, anyB } from './b';
// $ExpectType any
unknownA;
// $ExpectType any
anyA;
// $ExpectType unknown
unknownB;
// $ExpectType any
anyB; c.ts ( import { unknownA, anyA } from './a';
import { unknownB, anyB } from './b';
// $ExpectType unknown
unknownA;
// $ExpectType unknown
anyA;
// $ExpectType unknown
unknownB;
// $ExpectType any
anyB; I think the benefit of this is that:
|
If possible, I think remcohaszing's #46347 (comment) may actually work out more cleanly. The ability to simultaneously lighten the burden on authors and place control in the user's hands is pretty game changing. It would hopefully help reduce confusion too. (I feel like |
can we possibly have a strict compiler flag which just introduces an extra built in library file? one which overrides the usual one, by setting various types to e.g. have it override we could then just maintain that alongside the standard lib files? |
Suggestion
π Search Terms
any vs unknown, any strict, unknown strict, unknown as any, any as unknown in strict
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
We have a lot of cases where we'd like to have folks opt-in to
unknown
instead ofany
in the eslib, DOM, DT and modules. We've discussed internally about the idea of a third primitive, but that's tricky because it requires finding a good word to describe that, would break.d.ts
backwards compatibility and that's quite a fine-grained change.I propose that we have a flag which lets
unknown
beatany
in union narrowing, which we class as a part of the stricter than strict category.Today all the way back to 3.6:
A future release
π Motivating Example
Anywhere where we use
any
but would prefer to offer a way to provideunknown
instead, for example:JSON.parse
fetch
/axios
APIsnew Object
/Object.create
π» Use Cases
Where the ecosystem breakage for moving from
any
are too big to warrant the move. I think switching outany | unknown
is a pretty reasonable syntax which doesn't add a new concept to the language. It means anyone can adopt support without having to force updating TypeScript versions.The text was updated successfully, but these errors were encountered: