David Yunevich
David Yunevich

Reputation: 194

Typescript generic bound any object except any class

How to make a generic bound which excludes any class instances, but accepts any other object?

class A {}
class B {
  name: string = '1'
}
interface C {
 name: string
}

// There should be a proper bound
function fn<T extends Record<string, any>>(v: T) {}

const a = new A()
const b = new B()
const c: C =  {name: '1'}

fn(a) // should be error
fn(b) // should be error
fn(c) // should be ok

After a lot of googling I think it's just not possible. But I hope I'm wrong.

Upvotes: 0

Views: 38

Answers (0)

Related Questions