Typescript 区分普通对象和类类型

Typescript 区分普通对象和类类型,typescript,Typescript,如何区分普通对象类型和类类型 class Foo { constructor(public a: number) {} } declare let obj: {a: number} declare let inst: Foo // Either one would be fine. type IsPlainObject<T> = ??? type IsClassType<T> = ??? 相关问题 我认为您必须查看原型,可能在类型系统中不可能。一个类唯

如何区分普通对象类型和类类型

class Foo {
  constructor(public a: number) {}
}

declare let obj: {a: number}
declare let inst: Foo

// Either one would be fine.
type IsPlainObject<T> = ???
type IsClassType<T> = ???

相关问题

我认为您必须查看原型,可能在类型系统中不可能。一个类唯一的区别是除了基
对象
之外的其他东西在原型链中,对吗?@H.B.没错。我已经在运行时这样做了,但是如果我不在类型级别也这样做,类类型就会被映射类型破坏。那么@WilliamLohan呢?问题不是运行时类型检查。谢谢你的努力!这不是严格的运行时检查,请参见第14行的
param
鼠标,但如果不是您要查找的内容,则可以理解
class Foo { a?: number }
class Bar { a?: number }

type A = Foo extends Bar ? 1 : 0  // => 1
type B = Bar extends Foo ? 1 : 0  // => 1

type Obj = { a?: number }

type C = Foo extends Obj ? 1 : 0  // => 1
type D = Obj extends Foo ? 1 : 0  // => 1