首页 > 其他分享 >[Typescript 5.0] const Type Parameters with readonly

[Typescript 5.0] const Type Parameters with readonly

时间:2023-01-30 15:56:29浏览次数:58  
标签:5.0 Typescript const string Alice readonly names type

https://devblogs.microsoft.com/typescript/announcing-typescript-5-0-beta/#exhaustive-switch-case-completions

 

In the example code:

type HasNames = { names: readonly string[] };
function getNamesExactly<T extends HasNames>(arg: T): T["names"] {
    return arg.names;
}

// Inferred type: string[]
const names = getNamesExactly({ names: ["Alice", "Bob", "Eve"]}) ;

Before TS 5.0, namesis string[]. 

 

 

There was a way to handle in this case as const

// Correctly gets what we wanted:
//    readonly ["Alice", "Bob", "Eve"]
const names2 = getNamesExactly({ names: ["Alice", "Bob", "Eve"]} as const);

with as const, we need to make sure it is {names: readonly string[]} instead of {readonly names: string[]}, the string[] has to ben readonly, in order to infer the string literl type.

 

Typescript 5.0

We can use const in generic type, to acheive the effect as as const

<const T extends HasNames>
type HasNames = { names: readonly string[] }; //readonly here is important
function getNamesExactly<const T extends HasNames>(arg: T): T["names"] {
    return arg.names;
}

// Correctly gets what we wanted:
//    readonly ["Alice", "Bob", "Eve"]
const names = getNamesExactly({ names: ["Alice", "Bob", "Eve"]}) ;

 

const Type parameters has to be used within the function call

Works

declare function fnGood<const T extends readonly string[]>(args: T): void;

// T is readonly ["a", "b", "c"]
fnGood(["a", "b" ,"c"]);

 

Not working

declare function fnGood<const T extends readonly string[]>(args: T): void;

const arr = ["a", "b" ,"c"]
// T is string[]
fnGood(arr);

 

 

---

Another example;

const routes = <const T extends string>(routes: T[]) => {
  const addRedirect = (from: T, to: T) => {}

  return {
    addRedirect
  }
}

const router = routes(["/users", "/posts", "/admin/users"])

router.addRedirect("/admin/users", "wef") // error: Argument of type '"wef"' is not assignable to parameter of type '"/users" | "/posts" | "/admin/users"'.

标签:5.0,Typescript,const,string,Alice,readonly,names,type
From: https://www.cnblogs.com/Answer1215/p/17076228.html

相关文章

  • 介绍 ComPDFKit 转换 SDK 1.5.0
    介绍ComPDFKit转换SDK1.5.0了解有关ComPDFKitPDFSDK的更多信息:https://www.compdf.com/ComPDFKitConversionSDK1.5.0已发布!该版本满足了用户PDF转RTF、PDF......
  • Vue3+TypeScript 项目中,配置 ESLint 和 Prettier
    接上篇:从0搭建vite-vue3-ts项目框架:配置less+svg+pinia+vant+axios文档同步项目gitee:https://gitee.com/lixin_ajax/vue3-vite-ts-pinia-vant-less.git 一、Eslint:用于......
  • zabbix-agent 离线安装5.0 centos7
    Centos7离线安装zabbix客户端环境:Centos7.71.下载离线安装包下载地址:http://repo.zabbix.com/zabbix在上述打开的页面中,依次选择版本号,环境,OS版本以及OS处理器型号。这......
  • TypeScript的super
    (function(){classAnimal{name:string;constructor(name:string){this.name=name;}sayHello(){console.log('动物再叫---');}}cl......
  • [Typescript] Understanding Generics at Different Levels of Functions
    Thefollowingcodeimport{expect,it}from'vitest';import{Equal,Expect}from'../helpers/type-utils';exportinterfaceCache<T>{get:(key:string......
  • FreeFileSync v5.0
    下载:​​​http://nchc.dl.sourceforge.net/project/freefilesync/freefilesync/v5.0/FreeFileSync_v5.0_setup.exe​​ ......
  • TypeScript踩坑记录
    设置experimentalDecorators无效errorTS1219:Experimentalsupportfordecoratorsisafeaturethatissubjecttochangeinafuturerelease.Setthe'experi......
  • 关于pacemaker中资源启动的位置约束Location Constraints
    默认情况,对于业务应用的资源启动在那里,可能是随机的、有时启动在app01上,也可能启动在app02了我们也可以通过手动配置分数的方式,将某个节点的分数配置到极高,无穷大,这样,资......
  • CentOS Linux release 7.6 zabbix5.0 安装
    #zabbix安装关方文档https://www.zabbix.com/cn/download?zabbix=5.0&os_distribution=centos&os_version=7&components=server_frontend_agent&db=mysql&ws=nginx安......
  • C++ const pointer
    在C++中const限定的指针类型常常令人困惑,现整理如下,以整型为例,主要区分如下三个例子constint*p;int*constp;constint*constp;其实就是2种情况,const在int前......