Type check between class and object literal in TypeScript

X. Liu

in TypeScript, an object literal can be assigned to a class typed variable if that object provides all properties and methods that the class needs.

class MyClass {
  a: number;
  b: string;
}

// Compiler won't complain
const instance: MyClass = { a: 1, b: '' };

// Compiler won't complain if I assign an object with more properties
const literal = { a: 1, b: '', c: false };
const instance2: MyClass = literal;

What I want to do here is to prevent this kind of assignment based on two reasons:

  1. instance instanceof MyClass should be true;
  2. I can assign an object with more properties (see above).

In this way, TypeScript class works more like an interface. Is there a way I can prevent this?

ecraig12345

From the TypeScript docs, what you're observing seems to be the expected behavior:

Type compatibility in TypeScript is based on structural subtyping. Structural typing is a way of relating types based solely on their members.

So if two types have the same structure, it's intended that objects of those types can be assigned to each other.

Workaround: private members

Once you start adding private members to a class (which you'd almost always do in practice), the type checking works much closer to how you want.

class MyClass {
  a: number;
  b: string;
  private c: number;
}

// "Property 'c' is missing in type '{ a: number; b: string; }' but required in type 'MyClass'."
const instance: MyClass = { a: 1, b: '' };

// "Property 'c' is private in type 'MyClass' but not in type '{ a: number; b: string; c: number; }'"
const literal: MyClass = { a: 1, b: '', c: 3 };

class OtherClass {
  a: number;
  b: string;
  private c: number;
}

// "Types have separate declarations of a private property 'c'"
const otherClass: MyClass = new OtherClass();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Simpler way to check literal object type in TypeScript

Typescript type to object literal

Type definition in object literal in TypeScript

Class type check in TypeScript

TypeScript: "extends" string literal type check not working

CSS class map and Template Literal Type typescript

Typescript cannot assign object literal to a generic type

How to declare a type from an object literal in typescript?

TypeScript Empty Object Literal Type Annotation

TypeScript object literal type assertion with generics

Assigning an object literal to a typescript generic type

Specifying the type of the value in an object literal in Typescript?

Typescript treating a object literal as a contract interface / type

Typescript template literal type from object array

Typescript check if object matches type

typescript type is lost on an object literal assign using a union type

Typescript: ensure object literal extends interface but return real object type

How to check if the type of an object is a class?

Check if string is present in String Literal Union Type Typescript

Check if an object is a type of a generic type in Typescript

Is it possible to define a type (string literal union) within a class in TypeScript?

Can you declare a object literal type that allows unknown properties in typescript?

Dynamic return type based on object property using string literal in TypeScript

TypeScript: Unexpected Assignment of Computed Property with Union Type in Object Literal

Typescript create key constrained type for object literal with inferred number values

How to specify type of an arrow function defined as an object literal in TypeScript?

Typescript constrain generic to string literal type for use in computed object property

Typescript. How to specify a type of a subset of object literal properties?

How can I type a method with 'this' inside object literal in typescript?