首页 > 其他分享 >[Typescript] 101. Hard - Typed Get

[Typescript] 101. Hard - Typed Get

时间:2022-11-15 00:44:42浏览次数:63  
标签:Typescript _____________ Get Typed Expect foobar world type

The get function in lodash is a quite convenient helper for accessing nested values in JavaScript. However, when we come to TypeScript, using functions like this will make you lose the type information. With TS 4.1's upcoming Template Literal Types feature, properly typing get becomes possible. Can you implement it?

For example,

type Data = {
  foo: {
    bar: {
      value: 'foobar',
      count: 6,
    },
    included: true,
  },
  hello: 'world'
}
  
type A = Get<Data, 'hello'> // 'world'
type B = Get<Data, 'foo.bar.count'> // 6
type C = Get<Data, 'foo.bar'> // { value: 'foobar', count: 6 }
 

Accessing arrays is not required in this challenge.

 

/* _____________ Your Code Here _____________ */

type Get<T extends Record<PropertyKey, any>, K extends string> = K extends keyof T
  ? T[K]
  : K extends `${infer P}.${infer U}`
    ? Get<T[P], U>
    : never;

/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Get<Data, 'hello'>, 'world'>>,
  Expect<Equal<Get<Data, 'foo.bar.count'>, 6>>,
  Expect<Equal<Get<Data, 'foo.bar'>, { value: 'foobar'; count: 6 }>>,
  Expect<Equal<Get<Data, 'foo.baz'>, false>>,

  Expect<Equal<Get<Data, 'no.existed'>, never>>,
]

type Data = {
  foo: {
    bar: {
      value: 'foobar'
      count: 6
    }
    included: true
  }
  'foo.baz': false
  hello: 'world'
}

 

标签:Typescript,_____________,Get,Typed,Expect,foobar,world,type
From: https://www.cnblogs.com/Answer1215/p/16891084.html

相关文章

  • DynamicTokenGet
    1packagecom.-.it.regioc.utils;23importcom.alibaba.fastjson.JSON;4importcom.alibaba.fastjson.JSONObject;5importcom.-.it.regioc.bean.config.Ht......
  • Feign 实现 GET 方法传递 POJO
    Feign实现GET方法传递POJO作者:Grey原文地址:博客园:Feign实现GET方法传递POJOCSDN:Feign实现GET方法传递POJO需求SpringMVC支持GET方法直接绑定POJO......
  • 什么是Session;request.getSession().setAttribute()
    Session:会话控制,是服务器为了保存用户状态而创建的一个特殊的对象。=>用于存储信息的一个对象   当我们在服务端使用session时,首先要获取session,下面这个图就对服务......
  • linux下使用wget命令提示cannot verify cmake.org's certificate
      1、在使用linux下载cmake编译器时,提示如上图错误可以看到上图提示,使用--no-check-certificate参数即可 ......
  • TypeScript系列 -> 遇到报错 Cannot find name ‘console‘. Do you need to change y
    学习ts遇到的报错Cannotfindname‘console‘.Doyouneedtochangeyourtargetlibrary?gingthe‘lib‘compileroption解决办法:需要安装TypeScript助手的运......
  • LoggerFactory.getLogger 的过程(一)
    Loggerlog=LoggerFactory.getLogger(TestRunner.class);上面这行代码,就可以获取一个logger的实例,然后就可以用来打印日志了。这篇就探究这个方法都干了什么。1publicsta......
  • Nuget管理器下载插件出现依赖性相关无法正确下载
    话不多说,直接上图:  上面我采用的是PM控制台下载EntityFrameWork最新版本,之所以要这样做,是因为利用Nuget管理器好像不咋管用呀,一直说这个依赖那个依赖啥的,还不如最底层......
  • [Typescript] 100. Hard - IsAny
    Sometimesit'susefultodetectifyouhaveavaluewith any type.Thisisespeciallyhelpfulwhileworkingwiththird-partyTypescriptmodules,whichcanex......
  • C语言 #define 和 typedef 区别
    在C语言编程中,typedef和 #define是最常用语句,可能很多工作过几年的工程师都没有去深究过它们的一些用法和区别。typedef的用法在C/C++语言中,typedef常用来定义一个标识......
  • jquery-一行代码解决跨域问题 | 仅限get请求
    问题平时我们在开发时遇到的跨域问题,后台暂不给解决时,我们一般可以用vue代理(node)、nginx反向代理等方式来解决但是当项目要上线的时候,这些问题将又会出现就比如,自制的......