首页 > 其他分享 >[Typescript] 98. Medium - Append to object

[Typescript] 98. Medium - Append to object

时间:2022-11-12 17:12:56浏览次数:35  
标签:Typescript _____________ sun value 98 Medium Expect key type

Implement a type that adds a new field to the interface. The type takes the three arguments. The output should be an object with the new field.

For example

type Test = { id: '1' }
type Result = AppendToObject<Test, 'value', 4> // expected to be { id: '1', value: 4 }

 

/* _____________ Your Code Here _____________ */
type Merge<T extends Record<PropertyKey, any>> = {
  [Key in keyof T]: T[Key]
} 
type AppendToObject<T extends Record<PropertyKey, any>, U extends string | number | symbol, V extends any> = Merge<T & {
  [Key in U]: V  
}>

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

type test1 = {
  key: 'cat'
  value: 'green'
}

type testExpect1 = {
  key: 'cat'
  value: 'green'
  home: boolean
}

type test2 = {
  key: 'dog' | undefined
  value: 'white'
  sun: true
}

type testExpect2 = {
  key: 'dog' | undefined
  value: 'white'
  sun: true
  home: 1
}

type test3 = {
  key: 'cow'
  value: 'yellow'
  sun: false
}

type testExpect3 = {
  key: 'cow'
  value: 'yellow'
  sun: false
  isMotherRussia: false | undefined
}

type cases = [
  Expect<Equal<AppendToObject<test1, 'home', boolean>, testExpect1>>,
  Expect<Equal<AppendToObject<test2, 'home', 1>, testExpect2>>,
  Expect<Equal<AppendToObject<test3, 'isMotherRussia', false | undefined>, testExpect3>>,
]

 

标签:Typescript,_____________,sun,value,98,Medium,Expect,key,type
From: https://www.cnblogs.com/Answer1215/p/16884174.html

相关文章

  • Typescript类型体操 - Unique
    题目中文实现类型的Lodash.uniq,Unique接受数组T,返回没有重复值的数组TEnglishImplementthetypeversionofLodash.uniq,UniquetakesanArrayT,returns......
  • Typescript类型体操 - LastIndexOf
    题目中文实现类型的Array.lastIndexOf,LastIndexOf<T,U>接受泛型参数ArrayT和anyU并返回数组T中最后一个U的索引示例:typeRes1=LastIndexOf<[1,2,3......
  • 198 - Docker+Kubernetes(k8s)微服务容器化实践
                生成md5代码  生成token代码 ......
  • 【TS】1103- 30个小知识让你更清楚TypeScript
    TypeScript是Microsoft开发的JavaScript的开源超集,用于在不破坏现有程序的情况下添加附加功能。由于其独特的优势,例如,静态类型和许多速记符号,TypeScript现在被前端和......
  • 【框架】984- 2021 年最佳 JavaScript 框架
    作者|OliviaCuthbert译者|Sambodhi策划|刘燕据Stackoverflow的2021年开发者调查,JavaScript已连续第八年成为使用最多的语言,有67.7%的受访者选择它。之所以如......
  • [Typescript] Zod in actions
    import{z}from"zod";exportenumSUBTYPE{ABORT="abort",START="start",UPLOAD="upload",LOADING="loading",}exportconstTYPE="print"......
  • typescript装饰器
    属性装饰器参数exportdefaultfunction(proto,key){//两个参数}给属性增加metadataimport'reflect-metadata';exportdefaultfunction(label,type?......
  • [Typescript] 97. Hard - Capitalize Words
    Implement CapitalizeWords<T> whichconvertsthefirstletterof eachwordofastring touppercaseandleavestherestas-is.Forexampletypecapitalized......
  • 安装 TypeScript 并编译成JS
    官网:https://github.com/microsoft/TypeScriptTypeScript是一种由微软开发的开源、跨平台的编程语言。它是JavaScript的超集,最终会被编译为JavaScript代码。TypeScript......
  • 「题解」Codeforces 1098D Eels
    暴力是,每次挑出最小的两个合并。需要观察到没有产生贡献的次数很小。考虑最小的那个数的大小,如果一次合并没有产生贡献,那么最小的数至少\(\times2\).所以最多会有\(\mat......