Description
We've pulled in a new feature called --strictOptionalProperties
which sits under the --strict
family of flags. If we were to start TypeScript over again, we believe the behavior of strictOptionalProperties
would be on by default; however, this strictness option comes at a time when the community is much more mature with lots of existing code.
I've opened up this issue to discuss some broader concerns so that we can ensure that --strictOptionalProperties
ships smoothly and so we can hear some feedback from users.
Off the bat, here's a few of my own concerns:
Discoverability of the Break
A user who upgrades TypeScript with --strict
on currently has no idea why their code has broken. At best, they will get an error like
Type 'T | U | V | undefined' is not assignable to type 'T | U | V'.
Type 'undefined' is not assignable to type 'T | U | V'.
We can do better here, which is why I've opened up #44403.
Manual Upgrade Woes
The most permissive thing to do to fix breaks from --strictOptionalProperties
is to tack on an | undefined
onto every single ?
-marked optional property; however, this is incredibly tedious for an existing project.
We'd like to provide some automated tooling to help alleviate this, which is why I've opened up #44419.
Surrounding Community Upgrade Woes
There are really 3 places that are outside most users' control that need to be upgraded to work correctly with strictOptionalProperties
:
lib.d.ts
- DefinitelyTyped (
@types
packages) - Libraries shipping their own types
The last one is the hardest to solve; however, the first two are way more automatable, and we can have the core team help with it, but it's definitely time taken away from other work, and that's a delicate tradeoff.
I haven't created an issue to manage this one, but I think it might be worth distributing segments of lib.d.ts
and some number of top packages to add | undefined
to the definitions and see if that helps. Ideally, automation from #44419 could help. We'd also have to update the lib.dom.d.ts
generator to emit | undefined
on most optional properties.
I'd like to get @RyanCavanaugh's thoughts on this one.
Other Feedback
I'm curious to hear feedback from users on this one - is it a useful option? Do you feel good about it and its purpose? Did it catch any interesting bugs? Let us know!