Skip to content
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

Use TypeScript satisfies operator for option defaults #249

Merged
merged 1 commit into from Nov 19, 2022

Conversation

bmish
Copy link
Owner

@bmish bmish commented Nov 19, 2022

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:

const columns: {
    [key in COLUMN_TYPE]: string;
  } = {
    [COLUMN_TYPE.CONFIGS_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.

@bmish bmish merged commit 20973d4 into main Nov 19, 2022
@bmish bmish deleted the option-defaults-satisfies branch November 19, 2022 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant