美文网首页
TypeScript - 01 初探

TypeScript - 01 初探

作者: Lisa_Guo | 来源:发表于2019-12-11 14:34 被阅读0次

Typescript 是Javascript的一个子集,提供在编译期间的类型检查等额外功能,最终编译后的还是Javascript

// 安装ts
npm install typescript -g

// 编译ts文件,输出js
tsc main.ts

类型注释( type annotation)

为变量增加类型说明,格式为 variable:type
如下函数只接受string类型的参数,调用时传参不匹配则编译时报错

function greeter(person: string) {
    return "Hello, " + person;
}

let user = [0, 1, 2];

document.body.textContent = greeter(user);

编译报错:error TS2345: Argument of type 'number[]' is not assignable to parameter of type 'string'.

编译期间可检测出对变量赋值时类型匹配度

接口(interface)

interface是一个规范约定,定义了一个对象包含的属性。也就是定义了一个复杂数据类型

interface Person {
   firstName: string;
   lastName: string;
}

function greeter(person: Person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}

let user = { firstName: "Jane", lastName: "User" };

document.body.textContent = greeter(user);

编译后的js为

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

class

// public修饰符会自动创建同名属性变量
class Student {
  fullName: string;
  constructor(public firstName: string, public lastName: string) {
     this.fullName = firstName + ' ' + lastName
 }
 sayHello() {
   console.log(this.fullName + ' is saying hello')
 }
}

interface Person {
   firstName: string;
   lastName: string;
}

function greeter(person: Person) {
   return "Hello, " + person.firstName + " " + person.lastName;
}

let user = new Student("Jane", "M.");

document.body.textContent = greeter(user);

编译后文件为

var Student = /** @class */ (function () {
    function Student(firstName, lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.fullName = firstName + ' ' + lastName;
    }
    Student.prototype.sayHello = function () {
        console.log(this.fullName + ' is saying hello');
    };
    return Student;
}());
function greeter(person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}
var user = new Student("Jane", "M.");
document.body.textContent = greeter(user);

相关文章

  • TypeScript - 01 初探

    Typescript 是Javascript的一个子集,提供在编译期间的类型检查等额外功能,最终编译后的还是Jav...

  • typescript初探

    018年啦,不学习typescript就out啦,前几天vue的作者发布vue3.0的开发也会奔想ts,所以躲是躲...

  • 初探Typescript

    语言类型 强类型 弱类型 (类型安全维度)强类型有类型上的约束,不允许任意的隐式类型转换优势:错误更早暴露代码智能...

  • TypeScript 初探

    本文更新于 2020-02-06。 TypeScript (中文官网)是具有类型系统,且是 JavaScript ...

  • 1、TypeScript初探

    1、安装 配置淘宝源:npm config set registry https://registry.npm.t...

  • TypeScript 函数初探

    # 1. 函数声明 # 2. 函数传参 # 3. 可选参数 # 4. 默认可选参数 # 5. 剩余参数

  • Angular2进阶

    在之前《Angular2初探》一文中我们已经将Angular2基于webpack+Typescript的开发环境搭...

  • ProGuard 初探

    title: ProGuard 初探date: 2019-01-28 博客地址:ProGuard 初探 0x00 ...

  • 2019年诱人的 TypeScript 视频教程

    ​ typescript 01 intro.mp4 typescript 02 why.mp4 typescrip...

  • TypeScript 接口(interface)初探

    # 前言 在面向对象的编程中,接口是一种定义行为和动作的规范。在程序设计里面,接口起到限制和规范的作用,定义了某一...

网友评论

      本文标题:TypeScript - 01 初探

      本文链接:https://www.haomeiwen.com/subject/aazwwctx.html