Typescript generic bound any object except any class

33 Views Asked by At

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.

0

There are 0 best solutions below