什么是 Typescript 中的打字 [英] What are Typings in Typescript

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

问题描述

我是 Angular2 的新手,到目前为止我很享受,但有一个问题:打字.我无法弄清楚如何使用它们以及它们到底是什么.有些地方说要使用类型化,有些地方说要使用 npm 来安装定义.

I am new to Angular2 and I'm enjoying it so far but there is one problem: Typings. I can't figure out how to use them and what exactly they are. Some places say to use typings, others say to use npm to install definitions.

我糊涂了!!

推荐答案

JavaScript 是无类型的,这意味着我们可以无限制地传递和使用数据、对象和函数.我们可以编写代码来调用对象上不存在的方法,或者引用我们没有的变量.在编写代码时很难发现这些类型的错误,并且可能导致代码不稳定和有缺陷.对代码进行重大更改可能会变得困难且有风险,因为您无法立即看到某些更改是否与其他地方的其余代码冲突.

JavaScript is untyped, meaning that we can pass around and use data, objects and functions with no constraints. We can write code that calls methods that don't exist on an object, or reference variables that we don't have. These kinds of mistakes can be hard to discover when you are writing code, and it can lead to unstable and buggy code. Doing big changes of your code can become difficult and risky as you don't immediately see if some changes conflicts with the rest of the code somewhere else.

TypeScript 主要是将 types 添加到 JavaScript.这意味着 TypeScript 要求您准确描述对象和数据的格式.当您这样做时,这意味着编译器可以调查您的代码并发现错误.它可以看出您正在尝试使用错误类型的参数调用函数,或引用在当前范围内无法访问的变量.

TypeScript is mainly about adding types to JavaScript. That means that TypeScript requires you to accurately describe the format of your objects and your data. When you do that, that means that the compiler can investigate your code and discover errors. It can see that you are trying to call a function with the wrong kinds of arguments, or reference a variable that is not accessible in the current scope.

当您自己编写 TypeScript 时,代码的这种正式描述是代码本身的一部分.

When you write TypeScript yourself, this formal description of the code is part of the code itself.

但是,当您使用 jQuery 或 moment.js 等外部库时,该代码中没有类型信息.因此,为了将它与 TypeScript 一起使用,您还必须获得描述该代码类型的文件.这些是类型声明文件,通常带有文件扩展名.d.ts.幸运的是,人们已经为最常见的 javascript 库编写了这些类型的类型声明文件.

However, when you use external libraries like jQuery or moment.js, there are no information of the types in that code. So in order to use it with TypeScript, you also have to get files that describe the types of that code. These are the type declaration files, most often with the file extension name .d.ts. Fortunately people have written those kinds of type declaration files for most common javascript libraries out there.

Typings 只是安装这些文件的工具.现在最好的做法是只使用npm.

Typings was just a tool to install those files. It is now best practice to just use npm.

当您安装了这些文件后,基本上只意味着下载它们并将它们放置在您的项目中,TypeScript 编译器将理解*外部代码,您将能够使用这些库.否则你只会到处出错.

When you have installed those files, which basically only means downloading them and placing them in your project, the TypeScript compiler will understand* that external code and you will be able to use those libraries. Otherwise you would only get errors everywhere.

* 根据您设置和配置项目的方式,您可能需要配置 typescript 来专门查找这些文件,或者它可能无需您的任何配置即可工作.

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

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