From cf755c932f752e828fa15acf02bdd47f95b7dd3e Mon Sep 17 00:00:00 2001 From: Alex Iglesias Date: Tue, 13 Jun 2023 11:38:51 +0200 Subject: [PATCH] Added new `LooseAutocomplete` type --- .changeset/gentle-dots-jump.md | 5 +++++ docs/types.md | 17 +++++++++++++++-- src/types/LooseAutocomplete.ts | 12 ++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 .changeset/gentle-dots-jump.md create mode 100644 src/types/LooseAutocomplete.ts diff --git a/.changeset/gentle-dots-jump.md b/.changeset/gentle-dots-jump.md new file mode 100644 index 0000000..450c490 --- /dev/null +++ b/.changeset/gentle-dots-jump.md @@ -0,0 +1,5 @@ +--- +'@finsweet/ts-utils': minor +--- + +Added new `LooseAutocomplete` type diff --git a/docs/types.md b/docs/types.md index 13e1ef5..16a75e5 100644 --- a/docs/types.md +++ b/docs/types.md @@ -1,5 +1,5 @@ --- -prev: +prev: text: 'Type Guards' link: '/type-guards' next: @@ -9,11 +9,24 @@ next: # Types - ## `FormField` `FormField` is the Form Field element on Webflow +## `LooseAutocomplete` + +A type that allows for variable autocompletion with loose validation. + +Example: + +```typescript +const f = (a: LooseAutocomplete<'a' | 'b'>) => a; +f(''); // VSCode autocomplete shows 'a' and 'b' as options, but doesn't fail when a different option is provided. +f('a'); // Valid +f('b'); // Valid +f('c'); // Valid +``` + ## `MapEntries` `MapEntries` converts a `Map` type to its equivalent when performing `[...map.entries()]`. diff --git a/src/types/LooseAutocomplete.ts b/src/types/LooseAutocomplete.ts new file mode 100644 index 0000000..22cc49b --- /dev/null +++ b/src/types/LooseAutocomplete.ts @@ -0,0 +1,12 @@ +/** + * A type that allows for variable autocompletion with loose validation. + * @example + * ```typescript + * const f = (a: LooseAutocomplete<'a' | 'b'>) => a; + * f(''); // VSCode autocomplete shows 'a' and 'b' as options, but doesn't fail when a different option is provided. + * f('a'); // Valid + * f('b'); // Valid + * f('c'); // Valid + * ``` + */ +export type LooseAutocomplete = T | Omit;