ReplaceKeys
提出詳細
type ReplaceNode<Node,T,Y> = { [K in keyof Node]: K extends T ? K extends keyof Y ? Y[K] : never : Node[K] } type ReplaceKeys<U, T, Y> = U extends U ? ReplaceNode<U,T,Y> : never
提出日時 | 2023-09-15 01:12:08 |
---|---|
問題 | ReplaceKeys |
ユーザー | sankantsu |
ステータス | Accepted |
import type { Equal, Expect } from '@type-challenges/utils' type NodeA = { type: 'A' name: string flag: number } type NodeB = { type: 'B' id: number flag: number } type NodeC = { type: 'C' name: string flag: number } type ReplacedNodeA = { type: 'A' name: number flag: string } type ReplacedNodeB = { type: 'B' id: number flag: string } type ReplacedNodeC = { type: 'C' name: number flag: string } type NoNameNodeA = { type: 'A' flag: number name: never } type NoNameNodeC = { type: 'C' flag: number name: never } type Nodes = NodeA | NodeB | NodeC type ReplacedNodes = ReplacedNodeA | ReplacedNodeB | ReplacedNodeC type NodesNoName = NoNameNodeA | NoNameNodeC | NodeB type cases = [ Expect<Equal<ReplaceKeys<Nodes, 'name' | 'flag', { name: number; flag: string }>, ReplacedNodes>>, Expect<Equal<ReplaceKeys<Nodes, 'name', { aa: number }>, NodesNoName>>, ]