Typescript 中的类型和类有什么区别? [英] What is the difference between type and class in Typescript?

查看:29
本文介绍了Typescript 中的类型和类有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

typeclass 有什么区别?

type Point {
  x: number, y: number
}

let p = new Point();

以上结果:

'Point' 仅指一种类型,但在此处用作值.

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

为什么会这样?我当然不是使用 Point 作为值,而是使用它来实例化类型.

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

在什么情况下我需要使用 type 因为 class 不合适?

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

推荐答案

Typescript 有两个不同的宇宙,它们在某些方面有联系:值空间和类型空间.类型空间是定义类型并且类型被完全擦除并且在运行时不存在的地方.值空间包含值并且在运行时显然会存在.

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.

什么是类型?任何带有 type 关键字的定义都是类型以及接口、类声明枚举

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

您会注意到我在两个空格中都提到了类声明.类存在于类型空间和值空间中.这就是为什么我们可以在类型注释(let foo: ClassName)和表达式(ex new ClassName())中使用它们的原因.

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)

在您的特定情况下,Point 只是一种类型,我们可以在类型注释中使用的东西,而不是我们可以在需要运行时存在的表达式中使用的东西.在这种情况下,该类型很有用,因为它允许编译器从结构上检查对象文字是否可分配给 Point 类型:

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

如果你想创建一个类,你需要使用 class 关键字,因为这将创建一个不仅仅是类型的运行时值:

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)

这篇关于Typescript 中的类型和类有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆