Use TypeScript satisfies
operator for option defaults
#249
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Details
Throughout the codebase, we have objects which we use to define mappings. We typically want to ensure every value in an enum is mapped to some value and not forgotten (as could easily happen when implementing new notices, types, options, etc), so we use this syntax to ensure that any missing key in the object will cause a type error:
This works fine when every key maps to the same type of value.
But for our option defaults object, there are different types that each option can map to, including boolean, string, or array. When we want to access a particular option default, using the above syntax results in TypeScript thinking the option default could be any of boolean, string, or array, even though it actually has a known type.
The new TypeScript satisfies operator allows us to ensure the object contains all the right keys but without losing the known types for each particular entry in the object.
Waiting on prettier support for TypeScript 4.9: prettier/prettier#13516.
To use this new syntax, you have to select "Select TypeScript Version" in VSCode and choose the workspace version, since VSCode doesn't ship with TypeScript 4.9 yet.