[Typescript] Default value for Builder pattern - 04 (keyof {} -> never)
typescript for gt value 04 Default pattern Builder
2023-09-14 08:59:11 时间
From previous post, Builder pattern - 03
If we do the following changes:
- class TypeSafeStringMap<TMap extends Record<string, string> = {}> {
+ class TypeSafeStringMap<TMap extends Record<string, string>> {
It has a big impact of the codebase:
When we have the default value:
class TypeSafeStringMap<TMap extends Record<string, string> = {}> {}
const map = new TypeSafeStringMap()
// ^? TypeSafeStringMap<{}>
When we don't have the default value:
class TypeSafeStringMap<TMap extends Record<string, string>> {}
const map = new TypeSafeStringMap()
// ^? TypeSafeStringMap<Record<string, string>>
So, when we set/get the value, we will see the differences
class TypeSafeStringMap<TMap extends Record<string, string> = {}> {}
const map = new TypeSafeStringMap().set('name', 'abc')
// map: TypeSafeStringMap<Record<"name", string>>
class TypeSafeStringMap<TMap extends Record<string, string>> {}
const map = new TypeSafeStringMap().set("name", "abc")
// ^? TypeSafeStringMap<Record<string, string> & Record<"name", string>>
Take a close look of difference:
// with default {}
TypeSafeStringMap<Record<"name", string>>
// without default {}
TypeSafeStringMap<Record<string, string> & Record<"name", string>>
When we have getter function:
get(key: keyof TMap): string {
return this.map[key];
}
// without default value
type a = keyof Record<"name", string>
// ^? "name"
type b = keyof (Record<string, string> & Record<"name", string>)
// ^? string
b
is type string
, because string | "name"
results in string
type.
Why a
work fine, this is because
type c = keyof {}
// ^? never
type d = never | "name"
// ^? "name"
Tips:
type isNever<T> = [T] extends [keyof {}] ? true: false;
type e = isNever<never> // true
type f = isNever<false> // false
相关文章
- [Typescript] Defining exclusive properties with TypeScript
- [Typescript] 114. Hard - Camelize
- [Typescript] Making TypeScript Stick - 6 - infer, build a ConstructorArg Type
- [Typescript] Making TypeScript Stick - 5 - Extends
- [React Typescript 2022] Refactor a React Component using TypeScript
- [Typescript] Prevent Type Widening of Object Literals with TypeScript's const Assertions
- [React] Use React.ReactNode for the children prop in React TypeScript components and Render Props
- [Typescript] Default value for Builder pattern - 04 (keyof {} -> never)
- [Typescript] Be Specific for Better Inference
- [Typescript] 142. Extreme - Multiply
- [Typescript] 107. Medium - FindIndex
- [TypeScript] Typescript Intersection & Union Types
- [Typescript] Narrow the unknown Type with TypeScript's Assertion Functions
- [Typescript] Statically Type Unknown Values with TypeScript's unknown Type
- [Typescript] Specify Exact Values with TypeScript’s Literal Types
- [Vue +TS] Use Two-Way Binding in Vue Using @Model Decorator with TypeScript
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
- QCChart2D Charting for JavaScript/TypeScript