有没有办法在typescript中显示类型的规范化版本?

有没有办法在typescript中显示类型的规范化版本?,typescript,visual-studio-code,Typescript,Visual Studio Code,typescript中使用了大量参数化类型别名,有时很难确定所有抽象层下的实际类型。是否有某种方法可以规范化一个具体的类型并从中删除所有类型别名 例如,如果我有以下类型: type Maybe<T> = T | null type Num = number type Base = { a: Num b: boolean c: Maybe<{ x: Num }> } type Hello = Pick<Base, 'a' | 'c'> 我还对是

typescript中使用了大量参数化类型别名,有时很难确定所有抽象层下的实际类型。是否有某种方法可以规范化一个具体的类型并从中删除所有类型别名

例如,如果我有以下类型:

type Maybe<T> = T | null
type Num = number
type Base = {
  a: Num
  b: boolean
  c: Maybe<{ x: Num }>
}

type Hello = Pick<Base, 'a' | 'c'>

我还对是否有一种方法可以进行浅层规范化并将Pick转换为


由于typescript使用结构类型Pick,扩展版本本质上是以不同的方式编写的同一类型,因此出于兼容性原因,我不会尝试扩展类型

如果您只想扩展类型来检查它,那么有一个技巧可以做到这一点。可以使用在具有{}的交集中键入的映射。这将使编译器扩展类型

这种行为没有记录在案,也不能保证将来会起作用。所以你可以使用它,但不要以任何有意义的方式依赖它

type Maybe<T> = T | null
type Num = number
type Base = {
  a: Num
  b: boolean
  c: Maybe<{ x: Num }>
}

type Hello = Pick<Base, 'a' | 'c'>


type Id<T> = {} & {
  [P in keyof T]: Id<T[P]>
}

type HelloExpand = Id<Hello>
// On hover you can see in the tooltip (pro tip, if you are quick you can copy a tooltip in VSCode and the playground):
// type HelloExpand = {
//     a: number;
//     c: {
//         x: number;
//     } | null;
// }


由于typescript使用结构类型Pick,扩展版本本质上是以不同的方式编写的同一类型,因此出于兼容性原因,我不会尝试扩展类型

如果您只想扩展类型来检查它,那么有一个技巧可以做到这一点。可以使用在具有{}的交集中键入的映射。这将使编译器扩展类型

这种行为没有记录在案,也不能保证将来会起作用。所以你可以使用它,但不要以任何有意义的方式依赖它

type Maybe<T> = T | null
type Num = number
type Base = {
  a: Num
  b: boolean
  c: Maybe<{ x: Num }>
}

type Hello = Pick<Base, 'a' | 'c'>


type Id<T> = {} & {
  [P in keyof T]: Id<T[P]>
}

type HelloExpand = Id<Hello>
// On hover you can see in the tooltip (pro tip, if you are quick you can copy a tooltip in VSCode and the playground):
// type HelloExpand = {
//     a: number;
//     c: {
//         x: number;
//     } | null;
// }


谢谢这正是我想要的。是的,我非常清楚结构类型,我想我在问题中已经讲清楚了。谢谢!这正是我想要的。是的,我非常清楚结构类型,我想我在问题中已经讲清楚了。
type Maybe<T> = T | null
type Num = number
type Base = {
  a: Num
  b: boolean
  c: Maybe<{ x: Num }>
}

type Hello = Pick<Base, 'a' | 'c'>


type Id<T> = {} & {
  [P in keyof T]: Id<T[P]>
}

type HelloExpand = Id<Hello>
// On hover you can see in the tooltip (pro tip, if you are quick you can copy a tooltip in VSCode and the playground):
// type HelloExpand = {
//     a: number;
//     c: {
//         x: number;
//     } | null;
// }