首页 > 其他分享 >[Typescript] 127. Hard - Assign

[Typescript] 127. Hard - Assign

时间:2022-12-05 15:02:52浏览次数:81  
标签:Typescript _____________ Hard a1 source Expect type Assign

You have a target object and a source array of objects. You need to copy property from source to target, if it has the same property as the source, you should always keep the source property, and drop the target property. (Inspired by the Object.assign API)

example

type Target = {
  a: 'a'
}

type Origin1 = {
  b: 'b'
}

// type Result = Assign<Target, [Origin1]>
type Result = {
  a: 'a'
  b: 'b'
}
type Target = {
  a: 'a'
  d: { 
    hi: 'hi'
  }
}

type Origin1 = {
  a: 'a1',
  b: 'b'
}


type Origin2 = {
  b: 'b2',
  c: 'c'
}

type Answer = {
   a: 'a1',
   b: 'b2',
   c: 'c'
   d: { 
      hi: 'hi'
  }
}

 

/* _____________ Your Code Here _____________ */
type MergeObject<T> = {
  [P in keyof T]: T[P]
} 
type Assign<T extends Record<string, unknown>, U> = U extends object[] 
  ? U extends [infer F, ...infer RT] 
    ? Assign<MergeObject<({
      [Key in keyof T as Key extends keyof F ? never: Key]: T[Key]
    } & {
      [Key in keyof F]: F[Key]
    })>, RT>
    : T
  :T


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

// case1
type Case1Target = {
}

type Case1Origin1 = {
  a: 'a'
}

type Case1Origin2 = {
  b: 'b'
}

type Case1Origin3 = {
  c: 'c'
}

type Case1Answer = {
  a: 'a'
  b: 'b'
  c: 'c'
}

// case2
type Case2Target = {
  a: [1, 2, 3]
}

type Case2Origin1 = {
  a: {
    a1: 'a1'
  }
}

type Case2Origin2 = {
  b: [2, 3, 3]
}

type Case2Answer = {
  a: {
    a1: 'a1'
  }
  b: [2, 3, 3]
}

// case3

type Case3Target = {
  a: 1
  b: ['b']
}

type Case3Origin1 = {
  a: 2
  b: {
    b: 'b'
  }
  c: 'c1'
}

type Case3Origin2 = {
  a: 3
  c: 'c2'
  d: true
}

type Case3Answer = {
  a: 3
  b: {
    b: 'b'
  }
  c: 'c2'
  d: true
}

// case 4
type Case4Target = {
  a: 1
  b: ['b']
}

type Case4Answer = {
  a: 1
  b: ['b']
}

type cases = [
  Expect<Equal<Assign<Case1Target, [Case1Origin1, Case1Origin2, Case1Origin3]>, Case1Answer>>,
  Expect<Equal<Assign<Case2Target, [Case2Origin1, Case2Origin2]>, Case2Answer>>,
  Expect<Equal<Assign<Case3Target, [Case3Origin1, Case3Origin2]>, Case3Answer>>,
  Expect<Equal<Assign<Case4Target, ['', 0]>, Case4Answer>>,
]

 

标签:Typescript,_____________,Hard,a1,source,Expect,type,Assign
From: https://www.cnblogs.com/Answer1215/p/16952266.html

相关文章

  • 我要涨知识——TypeScript 常见面试题(二)
    又是一个年底来了,好大一批人可能又准备跑路了,最近回家待产话不多说,赶紧开干,给自己整了一个前端面试小助手——微信小程序内搜索“WEB学习学习加油站”,整理了前端经典高频......
  • c++ vector resize 和 assign
    resize改变大小resize(n,value),不够的部分填充为valuevector<int>nums{1,2,3,4,5,6};nums.resize(3,100);//size缩小,保持原状->{1,2,3}nums.resi......
  • 使用ShardingCore分表
    项目环境:ABP+Mysql 一、准备一个ABP项目这里采用ABP的Samples中的一个样板项目TodoApp下的Mvc-EfCore下载地址:https://github.com/abpframework/abp-samples/tree/......
  • [Typescript] 126. Hard - Two Sum
    Givenanarrayofintegers nums andaninteger target,returntrueiftwonumberssuchthattheyaddupto target./*_____________YourCodeHere________......
  • Typescript类型题材 - NumberRange
    题目中文有时我们需要限制数字的范围...示例:typeresult=NumberRange<2,9>//|2|3|4|5|6|7|8|9EnglishSometimeswewanttolimittheran......
  • Typescript类型题材 - ConstructTuple
    题目中文构造一个给定长度的元组。例如typeresult=ConstructTuple<2>//期望得到[unknown,unkonwn]EnglishConstructatuplewithagivenlength.Forexam......
  • TypeScript中使用NodeJs日期格式化库myjs-common
    依赖包安装#安装myjs-common包[email protected]格式器表达式YEAR_FORMAT:年格式化-yyyyMONTH_FORMAT:月格式化-yyyy-MMDATE_FORMAT:日期格式化-yyyy-MM-ddH......
  • [Typescript] 125. Extreme - Object Key Paths
    Getallpossiblepathsthatcouldbecalledby _.get (alodashfunction)togetthevalueofanobjecttypeT1=ObjectKeyPaths<{name:string;age:number......
  • TypeScript 常见方法
    TypeScriptArray(数组)数组对象是使用单独的变量名来存储一系列的值。数组非常常用。假如你有一组数据(例如:网站名字),存在单独变量如下所示varsite1="Google";varsite2......
  • typescript学习总结
    typescript学习总结qq学习讨论群:910316886<!--安装:npmi-gtypescripttsc-v(查看typescript版本)将ts编译为js,在终端输入命令,tschello.ts执......