Some way to distinguish an object type that only has Object in its prototype chain.
In this library, we use mapped types to convert immutable objects/arrays into their mutable representation. We _never_ do this for class instances, so I need a way to skip the mapped type when an object type is actually a class type (in order to preserve readonly properties on class instances).
I have no suggestions for such syntax.
My suggestion meets these guidelines:
Treat class Foo {} differently than class Foo extends Object {} like so:
extends Object is omitted from the class declaration, make Foo extends Object evaluate to false. Otherwise, behave normally.class Foo {
private _: any
}
class Bar extends Object {
private _: any
}
declare const test: (arg: Object) => void
test(new Foo) // 馃挜
test(new Bar) // 馃憤
declare const test2: (arg: object) => void
test2(new Foo) // 馃憤
test2(new Bar) // 馃憤
Add a new Exact<T> type that forbids narrow class types.
T is a literal type, the resolved type is TT is Object, any object literal is allowed (but Object sub-classes are not)T is Array, any array literal is allowed (but Array sub-classes are not)T is Function, any function literal is allowed (but Function sub-classes are not)T is any other class, only instances of T are allowed (no sub-classes or super-classes)class Foo extends Object {
constructor(public a: number) {}
}
declare const testObject: (arg: Exact<Object>) => void
testObject(new Foo(1)) // 馃挜
testObject({ a: 1 }) // 馃憤
testObject(Object.create(null)) // 馃憤
declare const testArray: (arg: Exact<Array>) => void
class MyArray extends Array {}
testArray(new MyArray()) // 馃挜
testArray([]) // 馃憤
declare const testFunc: (arg: Exact<Function>) => void
class MyFunc extends Function {}
testFunc(new MyFunc()) // 馃挜
testFunc(() => 0) // 馃憤
declare const testFoo: (arg: Exact<Foo>) => void
class Cart extends Foo {}
testFoo(new Foo(1)) // 馃憤
testFoo(new Cart(2)) // 馃挜
__Related:__ #12936
@aleclarson - I'm facing the exact same issue in one of my libs. Please let me know if you figure any hacky workarounds. 鉂わ笍
I have a proxy which should not ever be used for classes. I need a way to say: "This function can take objects, but not ones which are classes"
Trying to solve this problem again, ended up back here.
@DanielRosenwasser @ahejlsberg Can this be brought up in a design meeting soon? 馃
Most helpful comment
@DanielRosenwasser @ahejlsberg Can this be brought up in a design meeting soon? 馃