question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Distinguish between plain object types and class types

See original GitHub issue

Search Terms

  • plain object class type

Suggestion

Some way to distinguish an object type that only has Object in its prototype chain.

Use Cases

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).

Examples

I have no suggestions for such syntax.

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

Related

https://stackoverflow.com/questions/53819550/distinguish-between-plain-objects-and-class-types#53819550

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:25
  • Comments:10

github_iconTop GitHub Comments

7reactions
aleclarsoncommented, Mar 24, 2020

@DanielRosenwasser @ahejlsberg Can this be brought up in a design meeting soon? 🤞

5reactions
aleclarsoncommented, Jan 21, 2019

Proposal B

Add a new Exact<T> type that forbids narrow class types.

Details

  • When T is a literal type, the resolved type is T
  • When T is Object, any object literal is allowed (but Object sub-classes are not)
  • When T is Array, any array literal is allowed (but Array sub-classes are not)
  • When T is Function, any function literal is allowed (but Function sub-classes are not)
  • When T is any other class, only instances of T are allowed (no sub-classes or super-classes)

Example

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Programmer dictionary: Class vs Type vs Object - Kt. Academy
Class is a blueprint or template from which objects are created. Object is an instance of a class. Here is a simple example:...
Read more >
Difference Between Object And Class - GeeksforGeeks
Class is a detailed description, the definition, and the template of what an object will be. But it is not the object itself....
Read more >
typescript - Distinguish between plain objects and class types
How can I distinguish a plain object type from a class type? class Foo { constructor(public a: number) {} } declare let obj:...
Read more >
Difference Between Class and Object in OOPs - Guru99
Key Differences between Class and Object · A class is a template for creating objects in a program, whereas the object is an...
Read more >
Objects and classes - Visual Basic | Microsoft Learn
Object members · Member Access · Fields and properties · Methods · Events · Instance members and shared members · Differences between classes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found