博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[转]TypeScript Quick start
阅读量:6376 次
发布时间:2019-06-23

本文共 5074 字,大约阅读时间需要 16 分钟。

本文转自:

Quick start

          

Get started with a simple TypeScript app.

Let’s get started by building a simple web application with TypeScript.

Installing TypeScript

There are two main ways to get the TypeScript tools:

  • Via npm (the Node.js package manager)
  • By installing TypeScript’s Visual Studio plugins

Visual Studio 2015 and Visual Studio 2013 Update 2 include TypeScript by default. If you didn’t install TypeScript with Visual Studio, you can still .

For NPM users:

npm install -g typescript

Building your first TypeScript file

In your editor, type the following JavaScript code in greeter.ts:

function greeter(person) {    return "Hello, " + person; } var user = "Jane User"; document.body.innerHTML = greeter(user);

Compiling your code

We used a .ts extension, but this code is just JavaScript. You could have copy/pasted this straight out of an existing JavaScript app.

At the command line, run the TypeScript compiler:

tsc greeter.ts

The result will be a file greeter.js which contains the same JavaScript that you fed in. We’re up and running using TypeScript in our JavaScript app!

Now we can start taking advantage of some of the new tools TypeScript offers. Add a : string type annotation to the ‘person’ function argument as shown here:

function greeter(person: string) { return "Hello, " + person; } var user = "Jane User"; document.body.innerHTML = greeter(user);

Type annotations

Type annotations in TypeScript are lightweight ways to record the intended contract of the function or variable. In this case, we intend the greeter function to be called with a single string parameter. We can try changing the call greeter to pass an array instead:

function greeter(person: string) { return "Hello, " + person; } var user = [0, 1, 2]; document.body.innerHTML = greeter(user);

Re-compiling, you’ll now see an error:

greeter.ts(7,26): Supplied parameters do not match any signature of call target

Similarly, try removing all the arguments to the greeter call. TypeScript will let you know that you have called this function with an unexpected number of parameters. In both cases, TypeScript can offer static analysis based on both the structure of your code, and the type annotations you provide.

Notice that although there were errors, the greeter.js file is still created. You can use TypeScript even if there are errors in your code. But in this case, TypeScript is warning that your code will likely not run as expected.

Interfaces

Let’s develop our sample further. Here we use an interface that describes objects that have a firstName and lastName field. In TypeScript, two types are compatible if their internal structure is compatible. This allows us to implement an interface just by having the shape the interface requires, without an explicit implements clause.

interface Person {    firstName: string;    lastName: string;}function greeter(person: Person) { return "Hello, " + person.firstName + " " + person.lastName; } var user = { firstName: "Jane", lastName: "User" }; document.body.innerHTML = greeter(user);

Classes

Finally, let’s extend the example one last time with classes. TypeScript supports new features in JavaScript, like support for class-based object-oriented programming.

Here we’re going to create a Student class with a constructor and a few public fields. Notice that classes and interfaces play well together, letting the programmer decide on the right level of abstraction.

Also of note, the use of public on arguments to the constructor is a shorthand that allows us to automatically create properties with that name.

class Student {    fullName: string;    constructor(public firstName, public middleInitial, public lastName) { this.fullName = firstName + " " + middleInitial + " " + lastName; } } interface Person { firstName: string; lastName: string; } function greeter(person : Person) { return "Hello, " + person.firstName + " " + person.lastName; } var user = new Student("Jane", "M.", "User"); document.body.innerHTML = greeter(user);

Re-run tsc greeter.ts and you’ll see the generated JavaScript is the same as the earlier code. Classes in TypeScript are just a shorthand for the same prototype-based OO that is frequently used in JavaScript.

Running your TypeScript web app

Now type the following in greeter.html:

    TypeScript Greeter    

Open greeter.html in the browser to run your first simple TypeScript web application!

Optional: Open greeter.ts in Visual Studio, or copy the code into the TypeScript playground. You can hover over identifiers to see their types. Notice that in some cases these types are inferred automatically for you. Re-type the last line, and see completion lists and parameter help based on the types of the DOM elements. Put your cursor on the reference to the greeter function, and hit F12 to go to its definition. Notice, too, that you can right-click on a symbol and use refactoring to rename it.

The type information provided works together with the tools to work with JavaScript at application scale. For more examples of what’s possible in TypeScript, see the Samples section of the website.

Visual Studio picture

转载地址:http://vxjqa.baihongyu.com/

你可能感兴趣的文章
realm java 0.9.0_java-找不到io.realm:realm-gradle-plugin:4.0.0.pr...
查看>>
python关键词对联_深度学习对联系统
查看>>
java jsp不显示数据库数据_jsp页面显示数据库的数据信息表
查看>>
java中project_Java Project引用Java project
查看>>
mac nginx php-fpm,Mac系统下搭建Nginx+php-fpm
查看>>
java策略模式使用场景,Java设计模式—策略模式
查看>>
oracle时钟,oracle RAC环境中系统时钟的调整
查看>>
oracle数据库体系结构ppt,Oracle数据库的体系结构
查看>>
linux 虚拟目录映射,IIS7添加虚拟目录映射另一台服务器的共享文件夹
查看>>
linux杀一个awk查出的进程,Linux shell - 找到进程pid,然后杀掉(jps, grep, awk)
查看>>
linux内核模块获取进程pid,内核模块打印进程线程PID信息
查看>>
linux共享文件夹sumba,linux 的 samba 实现共享文件夹
查看>>
linux查看设备使用状况,linux下查看硬件资源和网络资源的使用情况
查看>>
centos linux终端,CentOS7 Linux系统命令
查看>>
ip核在linux的驱动,基于嵌入式Linux的USBOTG IP核驱动的设计与实现
查看>>
嵌入式linux系统网络通信,基于嵌入式Linux系统中网络通信研究与实现
查看>>
vmware linux 无网络配置文件,vmware虚拟机添加新网卡后,/etc/sysconfig/network-scripts/下无配置文件ifcfg-xxxx...
查看>>
linux yum库地址,Linux 搭建本地yum仓库
查看>>
linux python htmlparser 安装,python pycparser安装程序
查看>>
linux调用接口带参数,系统调用之二:参数传递
查看>>