typescript 学习_噢,天哪,我不想学习TypeScript。

typescript 学习

Ok, we get it, you don’t want to learn TypeScript, but let me try to convince you.

好的,我们明白了,您不想学习TypeScript,但是让我尝试说服您。

Why should you use TypeScript?

为什么要使用TypeScript?

If you already have some experience using JavaScript I bet you sometimes feel that it’s messy, you would end up with a lack of reuse and maintainability.

如果您已经有使用JavaScript的经验,我敢打赌您有时会觉得它很杂乱,那么您最终将缺乏重用性和可维护性。

So if you want a maintainable code out of the box, then you should try TypeScript.

因此,如果您想要开箱即用的可维护代码,则应尝试使用TypeScript。

TypeScript it’s pretty straight forward if you come from javascript or any even something like java or C#, but if you don’t, I think you will get it right away.

TypeScript,如果您来自javascript或什至诸如java或C#之类的东西,这是非常简单的,但是如果您不是来自Java的人,我想您会马上得到它。

但是首先,什么是TypeScript? (But first, what is TypeScript?)

TypeScript is an open-source language that builds on JavaScript, one of the world’s most used tools, by adding static type definitions.

TypeScript是一种开放源代码语言,它通过添加静态类型定义在JavaScript(全球最常用的工具之一)上构建。

主要特点 (Key Features)

  • Supports standard Javascript, you will not need to rewrite all of you code base

    支持标准Javascript,您无需重写所有代码库
  • Provides static typing. free unit test for type checking.

    提供静态输入。 免费的单元测试,用于类型检查。
  • Encapsulation through classes and modules

    通过类和模块进行封装
  • Support interfaces 💛

    支持界面💛

In a few words TypeScript is a superset of JavaScript, so everything you already know about javascript still applies when you write TypeScript code,

简而言之, TypeScript是JavaScrip t 的超集 ,因此,当您编写TypeScript代码时,关于JavaScript的所有知识仍然适用,

I’m looking at you “Angular.js” 😡

我在看着你“ Angular.js”😡

Examples:

例子:

  • {} brackets will define code blocks

    {}括号将定义代码块
  • Semi-colons end code expressions (optional)

    分号结束代码表达式(可选)
  • Javascript keywords such as for, if, throw, etc.

    JavaScript关键字,例如for,if,throw等。

Here I will leave a list of keyword and operators you will start seeing when you start coding with TypeScript:

在这里,我将留下一个关键字和运算符的列表,当您开始使用TypeScript进行编码时,您将开始看到它们:

  • Class: Container for members such as properties, functions, etc

    类:成员的容器,例如属性,函数等

  • constructor: Provides initialization functionality in a class

    构造函数:在类中提供初始化功能

  • exports: export a single member from a module

    导出:从模块中导出单个成员

  • extends: extends a class or interface

    扩展:扩展类或接口

  • implements: implement an interface

    实现:实现一个接口

  • imports: import a module

    导入:导入模块

  • interface: Defines a code contract that can be implemented by types

    接口:定义可以按类型实现的代码协定

  • module: Container for classes and other code

    模块:类和其他代码的容器

  • public/private : Member visibility

    公共/私人 :成员可见度

  • <typeName>: use to cast / convert between types example: <String>

    <typeName>:用于在类型之间进行转换/转换,例如:<String>

  • “=>” : Arrow syntax used with definitions and functions

    “ =>”:用于定义和功能的箭头语法

  • “:” : Separator between variables/parameters name and types

    “:”:变量/参数名称和类型之间的分隔符

“代码层次结构” (“Code Hierarchy”)

If you want to call it that way

如果你想这样称呼

As you may know in JavaScript we have functions for everything, but let’s say that we can simulate a Hierarchy using TypeScript. From an overall perspective, we can look at it like this.

如您在JavaScript中可能知道的那样,我们为所有功能提供了功能,但可以说我们可以使用TypeScript来模拟层次结构。 从总体上看,我们可以这样看。

  • Module / Namespace

    模块/命名空间
  • Class

  • Fields

    领域
  • Constructor

    建设者
  • Properties

    物产
  • Functions

    功能

Note: A class can also implement Interfaces in cases that we want to drive consistency across multiple classes

注意:如果我们想在多个类之间实现一致性,则一个类也可以实现Interfaces

好的,我明白了,给我看一些代码。 (Ok, I get it, show me some code.)

Make sure that you have TypeScript installed on your computer:

确保您的计算机上安装了TypeScript:

$ npm install typescript --g

In this example we have `User.ts` file that defines a User class with name and id properties

在这个例子中,我们有`User.ts`文件,它定义了一个具有name和id属性的User类。

If you run:

如果您运行:

$ tsc User.ts

You will get a JavaScript file in the same directory named: `User.js` that would look like this.

您将在同一目录中找到一个JavaScript文件,该文件名为:User.js,如下所示。

结论 (Conclusion)

TypeScript is a great tool to have in your pocket, if you already have some skills in javascript I think you will love it or hate it but I will say just give it a try and you will fall in love I promise you, and if you are new at javascript I recommend that you can start with TypeScript but once you generate the javascript code take a look at it, that way you will have a better understanding of how javascript works.

TypeScript是一个很好的工具,如果您已经掌握了javascript技能,我想您会喜欢或讨厌它,但我会说只要尝试一下,您就会爱上我,我向您保证,如果您javascript是我的新手,我建议您可以从TypeScript开始,但是一旦生成了javascript代码,就可以看看它,这样您将对javascript的工作方式有更好的了解。

翻译自: https://medium.com/@chl03k/oh-hell-no-i-dont-want-to-learn-typescript-f2ca678bcdd4

typescript 学习

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值