Distinguish between plain object types and class types
See original GitHub issueSearch 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
Issue Analytics
- State:
- Created 5 years ago
- Reactions:25
- Comments:10
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@DanielRosenwasser @ahejlsberg Can this be brought up in a design meeting soon? 🤞
Proposal B
Add a new
Exact<T>
type that forbids narrow class types.Details
T
is a literal type, the resolved type isT
T
isObject
, any object literal is allowed (butObject
sub-classes are not)T
isArray
, any array literal is allowed (butArray
sub-classes are not)T
isFunction
, any function literal is allowed (butFunction
sub-classes are not)T
is any other class, only instances ofT
are allowed (no sub-classes or super-classes)Example
Related: #12936