typescript_TypeScript速成课程

typescript

Typescript is a typed superset of javascript which aims to ease the development of large javascript applications. Typescript adds common concepts such as classes, generics, interfaces and static types and allows developers to use tools like static checking and code refactoring.

Typescript是javascript的类型化超集,旨在简化大型javascript应用程序的开发。 Typescript添加了诸如类,泛型,接口和静态类型之类的通用概念,并允许开发人员使用诸如静态检查和代码重构之类的工具。

为什么要关心打字稿: (Why care about Typescript:)

Now the question remains why you should use Typescript in the first place. Here are some reasons why javascript developers should consider learning Typescript.

现在的问题仍然是,为什么您应该首先使用Typescript。 这是javascript开发人员应考虑学习Typescript的一些原因。

静态类型: (Static typing:)

Javascript is dynamically typed which means that it doesn’t know the type of your variable until it instantiates it at run-time which can cause problems and errors in your projects. Typescript adds static type support to Javascript which takes care of bugs that are caused by false assumption of a variable type if you use it right. You still have full control over how strict you type your code or if you even use types at all.

Javascript是动态类型的,这意味着它直到运行时实例化变量时才知道变量的类型,这会导致项目中的问题和错误。 Typescript在Javascript中添加了静态类型支持,可解决由于正确使用变量类型而导致的错误。 您仍然可以完全控制键入代码的严格程度,甚至根本不使用类型。

更好的IDE支持: (Better IDE support:)

One of the biggest advantages of Typescript over Javascript is the great IDE support which includes Intellisense, real-time information from the Typescript compiler, debugging and much more. There are also some great extensions to further boost your Typescript development experience.

Typescript优于Javascript的最大优势之一是对IDE的强大支持,其中包括Intellisense,来自Typescript编译器的实时信息,调试等。 还有一些很棒的扩展,可以进一步提升您的Typescript开发经验。

访问新的ECMAScript功能: (Access to new ECMAScript features:)

Typescript gives you access to the newest ECMAScript feature and transcripts them to the ECMAScript targets of your choice. That means that you can develop your applications using the newest tools without needing to worry about browser support.

Typescript使您可以访问最新的ECMAScript功能,并将其转录为您选择的ECMAScript目标。 这意味着您可以使用最新工具来开发应用程序,而不必担心浏览器支持。

什么时候应该使用它: (When should you use it:)

By now we should know why Typescript is useful and where it can improve our development experience. But it is not the solution to everything and certainly doesn’t prevent you from writing terrible code by itself. So let’s take a look at where you should definitely use Typescript.

到现在为止,我们应该知道Typescript为什么有用,并且可以在哪里改善我们的开发经验。 但这并不是解决所有问题的方法,当然也不能阻止您自己编写糟糕的代码。 因此,让我们看一下您绝对应该在哪里使用Typescript。

当您拥有大型代码库时: (When you have a large codebase:)

Typescript is a great addition to large codebase because it helps you prevent a lot of common errors. This especially applies if there are more developers working on a single project.

Typescript是大型代码库的重要补充,因为它可以帮助您防止许多常见错误。 如果有更多的开发人员在一个项目上工作,这尤其适用。

当您和您的团队已经知道静态类型的语言时: (When you and your team already know statically typed languages:)

Another obvious situation to use Typescript is when you and your team already know statically typed languages like Java and C# and don’t wanna change to writing Javascript.

使用Typescript的另一种明显情况是,当您和您的团队已经知道Java和C#等静态类型的语言并且不想更改为编写Javascript时。

建立: (Setup:)

To setup typescript, we just need to install it with the npm package manager and create a new typescript file.

要设置打字稿,我们只需要使用npm软件包管理器安装它并创建一个新的打字稿文件。

npm install -g typescript

After installing it we can continue looking at the syntax and features typescript provides us with.

安装后,我们可以继续查看打字稿为我们提供的语法和功能。

类型: (Types:)

Now let’s take a look at which types are available to us in Typescript.

现在让我们看一下Typescript中对我们可用的类型。

数: (Number:)

All numbers in Typescript are floating point values. All of them get the number type including binary and hex values.

Typescript中的所有数字均为浮点值。 它们全部都获得数字类型,包括二进制和十六进制值。

let num: number = 0.222;
let hex: number = 0xbeef;
let bin: number = 0b0010;
串: (String:)

As in other languages, Typescript uses the String Datatype to save textual data.

与其他语言一样,Typescript使用String数据类型保存文本数据。

let str: string = 'Hello World!';

You can also use a multiline string and embed expressions by surrounding your String with backticks ``

您还可以使用多行字符串并通过用反引号将String包围起来来嵌入表达式``

let multiStr: string = `A simple
multiline string!`
let expression = 'A new expression'
let expressionStr: string = `Expression str: ${ expression }`
布尔值: (Boolean:)

Typescript also supports the most basic datatype of all, the boolean, which can only be true or false.

Typescript还支持所有最基本的数据类型,布尔值,只能为true或false。

let boolFalse: boolean = false;
let boolTrue: boolean = true;

分配类型: (Assigning Types:)

Now that we have the basic datatypes down we can look at how you assi

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值