[Typescript] 98. Medium - Append to object
typescript to object Medium append 98
2023-09-14 09:00:44 时间
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装饰器从入门到应用
- 深入浅出 TypeScript
- typescript web_typescript文档
- TypeScript高级类型-Partial
- TypeScript基础知识
- Typescript 4.9重点特性探索
- 「使用 webpack 5 从0到1搭建React + TypeScript 项目环境」3. 资源模块
- TypeScript-tsx文件webstorm无法识别alias配置解决方法
- 快速搭建基于Vue3+Vite2+Arco+Typescript+Pinia后台管理系统模板,文末有福利
- to program_I Just Want To
- TypeScript笔记
- To add browser notifications to an online customer support live chat service system - gofly - self h
- 软件开发入门教程网站之TypeScript Array(数组)
- TypeScript不学?你养我啊[2]
- 关于 2022 年 JavaScript 生态调查报告:TypeScript持续主导,Vite和Tauri大受欢迎
- Typescript学习笔记,从入门到精通,持续记录
- Typescript 全栈最值得学习的技术栈 TRPC
- TypeScript 中提升幸福感的 10 个高级技巧