在尝试实现Pick
时,我不确定为什么WrongPick
类型会给|
带来糟糕的结果。
interface Todo {
title: string
description: string
completed: boolean
}
type WrongPick<T, K> = K extends keyof T ? {[P in K]: T[P]} : never
type wrong_result = WrongPick<Todo, 'title' | 'completed'>
// expected: { title: string ; completed: boolean }
// actual: { title: string } | { completed: boolean }
type CorrectPick<T, K extends keyof T> = {[P in K]: T[P]}
type correct_result = CorrectPick<Todo, 'title' | 'completed'>
// expected: { title: string ; completed: boolean }
// actual: { title: string ; completed: boolean }
打字机Playground
1条答案
按热度按时间wn9m85ua1#
由于分布式条件类型,
WrongPick<Todo, "title" | "completed">
实际上扩展为其简化为
如上述文档链接所述,您可以通过将两端 Package 在元组中来"关闭它":
Playground