首页 > 其他分享 >[Typescript] Default value for Builder pattern - 04 (keyof {} -> never)

[Typescript] Default value for Builder pattern - 04 (keyof {} -> never)

时间:2023-02-14 15:25:47浏览次数:47  
标签:map Typescript name 04 Default TypeSafeStringMap keyof type class

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

标签:map,Typescript,name,04,Default,TypeSafeStringMap,keyof,type,class
From: https://www.cnblogs.com/Answer1215/p/17119667.html

相关文章

  • 04-《AspNetCore》-Options
    Options视频讲解package说明ASP.NETCore中的选项模式|MicrosoftLearnMicrosoft.Extensions.Options:选项的核心包,扩展IServiceCollection接口,只支持内存配置。Mic......
  • [Typescript] Builder pattern - 03
    import{expect,it}from'vitest';classTypeSafeStringMap<TMapextendsRecord<string,string>={}>{privatemap:TMap;constructor(){this.map=......
  • 3604、猜数字大小
    猜数字游戏的规则如下:每轮游戏,我都会从1到n随机选择一个数字。请你猜选出的是哪个数字。如果你猜错了,我会告诉你,你猜测的数字比我选出的数字是大了还是小了。你可以通......
  • Vue props中的default值正确写法
    先看一个警告[Vuewarn]:Invaliddefaultvalueforprop"content":PropswithtypeObject/Arraymustuseafactoryfunctiontoreturnthedefaultvalue.//错......
  • VMware 虚拟机安装 Ubuntu 20.04-server
    一、ubuntu镜像下载选择服务器版:https://cn.ubuntu.com/download/server/step1 二、安装步骤1、选择语言:选择语言为English 2、系统更新:选择不更新 3、设置键......
  • optee km4.0 VTS: PerInstance/SigningOperationsTest.RsaUseRequiresCorrectAppIdApp
    异常日志:#./VtsHalKeymasterV4_0TargetTest--gtest_filter=PerInstance/SigningOperationsTest.RsaUseRequiresCorrectAppIdAppData/0_defaultNote:GoogleTestfilter......
  • Navicat远程连接linux下mysql服务器1045错误解决办法在这儿
    1:首先通过xshell工具或者你熟悉的工具连接远程linux下的服务器mysql-uroot-p   然后输入密码 2.进行授权如果想root用户使用password从任何主机连接到mysql服务器......
  • 104、工单,是否可用?怎么回答
    一、问题crm工单,是否可用(潜在信息,crm工单具体指在“新dts”系统)二、答案1、80分答案可用。有一定的局限性:crm的工单条数,在老dts和新dts都有效,存在2份。后续会优化。后续可......
  • react typescript封装函数式组件styled-component
    interfaceIButton{margin?:boolean;width?:string;justify?:JustifyContentProps;}constButton=styled.button<IButton>`.....`;interfaceILoadButtonext......
  • 04 如何进行数据表表分区? | OushuDB 数据库使用入门
    表分区在数据库日渐庞大的今天,为了方便对数据库数据的管理,比如按时间、地区去统计一些数据时,基数过于庞大带来了诸多不便。很多商业数据库都提供分区的概念,按不同的维度去存......