Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between type and class in Typescript?

Tags:

typescript

What is the difference between type and class?

type Point {   x: number, y: number }  let p = new Point(); 

The above results in:

'Point' only refers to a type, but is being used as a value here.

Why is this so? I am not certainly not using Point as a value but using it to instantiate a type.

What are the situations where I would need to use type because class is not suitable?

like image 602
Old Geezer Avatar asked Jul 02 '18 08:07

Old Geezer


People also ask

What is difference between class and type?

The class defines object's internal state and the implementation of its operations. In contrast, an object's type only refers to its interface - a set of requests to which it can respond. An object can have many types, and objects of different classes can have the same type.

Should I use type or class TypeScript?

When should you use types in TypeScript? Unlike classes, types do not express functionality or logic inside your application. It's best to use types when you want to describe some form of information. They can describe varying shapes of data, ranging from simple constructs like strings, arrays, and objects.

Is class A type in TypeScript?

When using the class keyword in TypeScript, you are actually creating two things with the same identifier: A TypeScript interface containing all the instance methods and properties of the class; and. A JavaScript variable with a different (anonymous) constructor function type.

What is a type in TypeScript?

What is a type in TypeScript. In TypeScript, a type is a convenient way to refer to the different properties and functions that a value has. A value is anything that you can assign to a variable e.g., a number, a string, an array, an object, and a function.


1 Answers

Typescript has two different universes that come into contact in some points: Value space and Type space. Type space is where types are defined and types get erased completely and don't exist at runtime. Value space contains values and will obviously exist at runtime.

What is a value? Value literals, variables, constants and parameters are obviously values. Functions and class declarations are also values as they do have a runtime object backing them up, namely the function object and the class constructor (also a function). Enums are also values as they are backed up by an object at runtime.

What is a type? Any definition with a type keyword is a type as well as interfaces, class declarations and enums

You will notice I mentioned class declarations in both spaces. Classes exist in both type space, and value space. This is why we can use them both in type annotations (let foo: ClassName) and in expressions (ex new ClassName()).

Enums also span both worlds, they also represent a type we can use in an annotation, but also the runtime object that will hold the enum.

Names in type space and value space don't collide, this is why we can define both a type and a variable with the same name:

type Foo = { type: true } var Foo = { value : true } // No error, no relation to Foo just have the same name in value space  

Class declarations and enums, since they span both spaces will 'use up' the name in both spaces and thus we can't define a variable or a type with the same name as a class declaration or enum (although we can do merging but that is a different concept)

In your specific case, Point is just a type, something we can use in type annotations, not something we can use in expressions that will need to have a runtime presence. In this case the type is useful as it allows the compiler to structurally check that the object literal is assignable to the Point type:

let p: Point = { x: 10, y: 15 }; // OK let p: Point = { x: 10, y: 15, z: 10 }; // Error 

If you want to create a class, you will need to do that with the class keyword, as that will create a runtime value that is not just a type:

class Point{     constructor(public x: number, public y: number){} } let p = new Point(10,10) 
like image 89
Titian Cernicova-Dragomir Avatar answered Oct 04 '22 17:10

Titian Cernicova-Dragomir