You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a flag which priorities unknown over any in any | unknownΒ #46347
We have a lot of cases where we'd like to have folks opt-in to unknown instead of any 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 beat any in union narrowing, which we class as a part of the stricter than strict category.
Today all the way back to 3.6:
typeA=any|unknown// ^? - type A = any
A future release
// @thisCompilerFlag: truetypeA=any|unknown// ^? - type A = unknown
π Motivating Example
Anywhere where we use any but would prefer to offer a way to provide unknown instead, for example:
Where the ecosystem breakage for moving from any are too big to warrant the move. I think switching out any | 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.
I just though of an alternative solution which turns out to be really close to #45605
Add a compiler option to forcefully turn any or unknown types which originate from a .d.ts file into either any or unknown when it reaches user code (.ts file). A user could still decide to use the any or unknown type in their own code. I think this also aligns, or could even supersede "useUnknownInCatchVariables".
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 any | unknown could be confusing for some... and it would require some updates to the docs.)
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 unknown
e.g. have it override JSON.parse to return unknown
we could then just maintain that alongside the standard lib files?
Activity
orta commentedon Oct 13, 2021
Could maybe also be used for things like:
unknowninstead ofanywhen failing to resolve a typeΒ #46330remcohaszing commentedon Oct 15, 2021
I just though of an alternative solution which turns out to be really close to #45605
Add a compiler option to forcefully turn
anyorunknowntypes which originate from a.d.tsfile into eitheranyorunknownwhen it reaches user code (.tsfile). A user could still decide to use theanyorunknowntype in their own code. I think this also aligns, or could even supersede"useUnknownInCatchVariables".For example, given:
a.d.ts:
b.ts:
This would be the behaviour:
c.ts (
{ "anyOrUnknownLibType": "as-is" }default)c.ts (
{ "anyOrUnknownLibType": "any" })c.ts (
{ "anyOrUnknownLibType": "unknown" })I think the benefit of this is that:
ITenthusiasm commentedon Oct 15, 2021
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
any | unknowncould be confusing for some... and it would require some updates to the docs.)43081j commentedon Feb 21, 2023
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
unknowne.g. have it override
JSON.parseto returnunknownwe could then just maintain that alongside the standard lib files?