yunliuyan / type-challenges

typescript-challenges
0 stars 2 forks source link

00005-easy-pick #5

Open yunliuyan opened 11 months ago

yunliuyan commented 11 months ago

实现 Pick 简单 #union #built-in

by Anthony Fu @antfu

接受挑战    English 日本語 한국어

欢迎 PR 改进翻译质量。

实现 TS 内置的 Pick<T, K>,但不可以使用它。

从类型 T 中选择出属性 K,构造成一个新的类型

例如:

interface Todo {
  title: string
  description: string
  completed: boolean
}

type TodoPreview = MyPick<Todo, 'title' | 'completed'>

const todo: TodoPreview = {
    title: 'Clean room',
    completed: false,
}

测试案例:

import type { Equal, Expect } from '@type-challenges/utils'

type cases = [
  Expect<Equal<Expected1, MyPick<Todo, 'title'>>>,
  Expect<Equal<Expected2, MyPick<Todo, 'title' | 'completed'>>>,
  // @ts-expect-error
  MyPick<Todo, 'title' | 'completed' | 'invalid'>,
]

interface Todo {
  title: string
  description: string
  completed: boolean
}

interface Expected1 {
  title: string
}

interface Expected2 {
  title: string
  completed: boolean
}

返回首页 分享你的解答 查看解答

相关挑战

3・实现 Omit
yunliuyan commented 11 months ago

思路

遍历key值,判断key是否继承T

代码实现

 type MyPick<T, K> = {
    [key in keyof T as key extends K ? key : never]: T[key]
  }
wudu8 commented 11 months ago
type EasyPick<T, U> = {
    [k in keyof T]: k extends U ? T[k] : never;
};

interface deepReadonlyType {
    name: string;
    description: string;
    address: string;
}

const easyPickTest: EasyPick<deepReadonlyType, 'name' | 'description'> = {
    name: "字节猿", // Ok
    description: "字节猿喜欢readonly", // Ok
    address: "我家在陕西", // Error: The expected type comes from property 'address' which is declared here on type 'EasyPick<deepReadonlyType, "name" | "description">'
};
Janice-Fan commented 11 months ago

type MyPick<T, P extends keyof T> = {

}

interface Todo { title: string description: string completed: boolean }

type TodoPreview = MyPick<Todo, 'title' | 'completed'>

const todo: TodoPreview = { title: 'Clean room', completed: false, }

liangchengv commented 11 months ago
type MyPick<T, K extends keyof T> = {
  [P in K]: T[P];
};
Naparte commented 11 months ago

type MyPick<T, K extends keyof T> = { [key in K]: T[key] }

interface Todo5 {
    title: string
    description: string
    completed: boolean
}

type TodoPreview = MyPick<Todo5, 'title' | 'completed'>

const todo5: TodoPreview = {
    title: 'Clean room',
    completed: false,
}