Coder Social home page Coder Social logo

typescript-tutorial's Introduction

next
introduction/README.md

TypeScript 入门教程

Actions Status

从 JavaScript 程序员的角度总结思考,循序渐进的理解 TypeScript。

关于本书

本书是作者在学习 TypeScript 后整理的学习笔记。

随着对 TypeScript 理解的加深和 TypeScript 社区的发展,本书也会做出相应的更新,欢迎大家 Star 收藏

  • 发现文章内容有问题,可以直接在页面下方评论
  • 对项目的建议,可以提交 issue 向作者反馈
  • 欢迎直接提交 pull-request 参与贡献

为什么要写本书

TypeScript 虽然有官方手册及其非官方中文版,但是它每一章都希望能详尽的描述一个概念,导致前面的章节就会包含很多后面才会学习到的内容,而有些本该一开始就了解的基础知识却在后面才会涉及。如果是初学者,可能需要阅读多次才能理解。所以它更适合用来查阅,而不是学习。

与官方手册不同,本书着重于从 JavaScript 程序员的角度总结思考,循序渐进的理解 TypeScript,希望能给大家一些帮助和启示。

由于一些知识点与官方手册重合度很高,本书会在相应章节推荐直接阅读中文手册。

关于 TypeScript

TypeScript 是 JavaScript 的一个超集,主要提供了类型系统对 ES6 的支持,它由 Microsoft 开发,代码开源于 GitHub 上。

它的第一个版本发布于 2012 年 10 月,经历了多次更新后,现在已成为前端社区中不可忽视的力量,不仅在 Microsoft 内部得到广泛运用,而且 Google 开发的 Angular 从 2.0 开始就使用了 TypeScript 作为开发语言,Vue 3.0 也使用 TypeScript 进行了重构。

适合人群

本书适合以下人群

本书不适合以下人群

  • 没有系统学习过 JavaScript
  • 已经能够很熟练的运用 TypeScript

评价

《TypeScript 入门教程》全面介绍了 TypeScript 强大的类型系统,完整而简洁,示例丰富,比官方文档更易读,非常适合作为初学者学习 TypeScript 的第一本书。

—— 阮一峰

版权许可

本书采用「保持署名—非商用」创意共享 4.0 许可证。

只要保持原作者署名和非商用,您可以自由地阅读、分享、修改本书。

详细的法律条文请参见创意共享网站。

相关资料

typescript-tutorial's People

Contributors

anxysuaen avatar bit0r avatar dengwb1991 avatar dependabot[bot] avatar geekxh avatar greenkeeper[bot] avatar gwuhaolin avatar huihuipan avatar jenkey2011 avatar jokester avatar ldwx avatar macaienlei avatar niices avatar nikolausliu avatar onebytetone avatar pd4d10 avatar pyvadev avatar ruochuan12 avatar tangdw avatar taxilng avatar usercao avatar wangyulue avatar wenbing avatar woai3c avatar wy-luke avatar xcatliu avatar yuchen100 avatar yuu2lee4 avatar zenghongtu avatar zitup avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

typescript-tutorial's Issues

能否提供付费答疑?

我是 https://wen-ma.com 的开发者,请问是否愿意提供付费答疑?

读者学习过程中遇到困难,向您支付50元,获得您20分钟的一对一帮助。

您可将网站的 定向提问链接 放在README.md中,方便读者直接对您提问。

我也不知道这算不算是广告,如有打扰请批评指正。

关于「代码检查」章节

使用 AlloyTeam 的 TSLint 配置」之下的「安装之后修改 tsconfig.json 即可」
是否是「安装之后修改 tslint.json 即可」

接口类型合并,合并后的接口怎么使用?

https://ts.xcatliu.com/advanced/declaration-merging

接口中方法的合并,与函数的合并一样:

interface Alarm {
    price: number;
    alert(s: string): string;
}
interface Alarm {
    weight: number;
    alert(s: string, n: number): string;
}

相当于:

interface Alarm {
    price: number;
    weight: number;
    alert(s: string): string;
    alert(s: string, n: number): string;
}

合并后的接口应该怎么使用?我自己尝试用,一直不对。🧐

interface Animal {
  walk(num: number): string;
}
interface Animal {
  walk(num: string): number;
}
// 相当于:
// interface Animal {
//   walk(num: number): string;
//   walk(num: string): number;
// }
// 接口合并了但是不会用合并后的接口🤔🤔🤔
// const dog: Animal = {
//   walk(num: string | number): string {
//     return 10;
//   }
// };

为什么要用接口来定义函数呢?

下面两种方法不是相等的吗? 那为什么 会有第一种写法呢? 第一种方法优势是什么? 谢谢

####################################################
Method 1
####################################################
interface CreateArrayFunc {
(length: number, value: T): Array;
}

let createArray: CreateArrayFunc;
createArray = function(length: number, value: T): Array {
let result = [];
for (let i = 0; i < length; i++) {
result[i] = value;
}
return result;
}

####################################################
Method 2
####################################################
let createArray = function(length: number, value: T): Array {
let result = [];
for (let i = 0; i < length; i++) {
result[i] = value;
}
return result;
}

null,undefined类型可以互相赋值。

例如:
let a:undefined = null;
let b:null = undefined;
静态检查不会报错,是否适合undefined类型可以赋值:null or undefined。 .同理null类型也是一样。
原文描述:
image

An in-range update of eslint-config-alloy is breaking the build 🚨

The devDependency eslint-config-alloy was updated from 2.0.5 to 2.1.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

eslint-config-alloy is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 12 commits.

  • fb0cc7c 2.1.0
  • 559b96f 升级 eslint, typescript-eslint, husky, rimraf
  • 92eafb9 Merge pull request #99 from AlloyTeam/dependabot/npm_and_yarn/mixin-deep-1.3.2
  • dedd0ae Bump mixin-deep from 1.3.1 to 1.3.2
  • 9062bb3 Merge pull request #98 from AlloyTeam/dependabot/npm_and_yarn/eslint-utils-1.4.2
  • 2317ebe Update docs, fix #92
  • 3320b93 Bump eslint-utils from 1.3.1 to 1.4.2
  • 548b5ad Add escape-xml
  • 2abf0ba Update insert-tag
  • c2a23ba Merge pull request #91 from AlloyTeam/dependabot/npm_and_yarn/lodash-4.17.14
  • 019606f Bump lodash from 4.17.11 to 4.17.14
  • 84a1284 Update comments

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

项目中使用到了ts吗?

看完了,写的很好。想问下作者,项目中使用到了ts吗?哪些场景下使用的。有没有demo学习一下

声明文件中的一些小问题

在声明文件-》什么是声明文件,
有这么一句话:
假如仍然无法解析,那么可以检查下 tsconfig.json 中的 files、include 和 exclude 配置,确保其包含了 jQuery.d.ts 文件。
这个例子我不管怎么配置 tsconfig.json ,在使用tsc XXX 在shell中运行的时候都会报错,说找不到对应的函数等等。(以及后面的全局变量的例子都是一样的)。能否给出一个可以完整通过编译的例子包括配置,这样可能会更有利于理解。

Global全局变量

你好

在js里想弄个全局变量一般直接global.foo = 'hello'; 使用的时候直接this.bar(foo);(这里假设是react native开发)
想重写全局方法会直接 global.console = ()=>{ alert('hello') } 使用的时候直接 console( );

想问下用了ts的话应该怎样做?

关于接口任意属性的一点疑问,对象属性不可以是联合类型(string | number)的吗?

interface Person { name: string; age?: number; [propName: string]: string; }

上面这个是示例。改成如下:

interface Person { name: string; age?: number; [propName:number|string]: number | string; }
就会报错:error TS1023: An index signature parameter type must be 'string' or 'number'.
看报错信息提示 类型必须是string或者 number。
使用联合类型方式就不可以?

元组章节的问题

let xcatliu: [string, number];
xcatliu[0] = 'Xcat Liu';
xcatliu[1] = 25;

xcatliu[0].slice(1);
xcatliu[1].toFixed(2);
这一段代码会实际上会报Variable 'xcatliu' is used before being assigned.

An in-range update of @typescript-eslint/eslint-plugin is breaking the build 🚨

The devDependency @typescript-eslint/eslint-plugin was updated from 1.9.0 to 1.10.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

@typescript-eslint/eslint-plugin is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build could not complete due to an error (Details).

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

元组篇章的初始化问题

let xcatliu: [string, number];
xcatliu[0] = 'Xcat Liu';
xcatliu[1] = 25;

xcatliu[0].slice(1);
xcatliu[1].toFixed(2);

这段代码经过编译后,是这样的。
var xcatliu;
xcatliu[0] = 'Xcat Liu';
xcatliu[1] = 25;
xcatliu[0].slice(1);
xcatliu[1].toFixed(2);
由于并没有把xcatliu改成数组类型,所以无法使用[0]和[1]对其进行赋值,无法找到属性,会报错。望修改。
顺便:作者能给个联系方式吗?

关于泛型的的描述

博客原文地址

根据官方网站的描述以及结合底下的例子
image

我认为应该这样改一下:

泛型(Generics)是指在定义函数、接口或类的时候,不预先指定具体的类型,
- 而在使用的时候再指定类型的一种特性
+ 而是在使用时才能确定类型,并保证传入的参数类型与返回值类型相同的一种特性。

tuple一章实际会报错

let xcatliu: [string, number];
xcatliu[0] = 'Xcat Liu';
xcatliu[1] = 25;

这里实际会报错【在赋值前使用了变量“xcatliu”。】
tsc -v 3.2.2

越界的元素中对越界元素的报错定义也有所不同了:

let xcatliu: [string, number];
xcatliu = ['Xcat Liu', 25];
xcatliu.push('http://xcatliu.com/');
xcatliu.push(true);

类型“true”的参数不能赋给类型“string | number”的参数。新版的ts没有Type 'boolean' is not assignable to type 'number'.这样的对应了。

函数重载疑问

function reverse(rest: number): number;
function reverse(rest: string): string;

function reverse (rest: number | string):number|string {
    if (typeof rest === 'number') {
       return Number(rest.toString().split('').reverse().join(''));
    }
    else if (typeof rest === 'string') {
        return rest.split('').reverse().join('');
    }
}
console.log(reverse(123))

在最后一次reverse函数实现的时候, function reverse (rest: number | string):number|string {}
最后的 number|string出错

数组类型一章中可以对索引类型进行补充,毕竟字符串也可做数组索引

可索引的类型

与使用接口描述函数类型差不多,我们也可以描述那些能够“通过索引得到”的类型,比如a[10]或ageMap["daniel"]。 可索引类型具有一个索引签名,它描述了对象索引的类型,还有相应的索引返回值类型。 让我们看一个例子

interface StringArray {
  [index: number]: string;
}

let myArray: StringArray;
myArray = ["Bob", "Fred"];

let myStr: string = myArray[0];

上面例子里,我们定义了StringArray接口,它具有索引签名。 这个索引签名表示了当用number去索引StringArray时会得到string类型的返回值。

更新 eslint 章节

typescript-eslint-parser 已弃用,需要更新为 @typescript-eslint/parser

泛型约束 extends 报错

代码在文章中的位置:
进阶 - 泛型 - 泛型约束报错

interface Lengthwise {
    length: number;
}

function loggingIdentity<T extends Lengthwise>(arg: T): T {
    console.log(arg.length);
    return arg;
}

报错Parsing error: Unexpected token, expected ","

image

字符串字面量类型

type EventNames = 'click' | 'scroll' | 'mousemove';
function handleEvent(ele: Element, event: EventNames) {
// do something
}

handleEvent(document.getElementById('hello'), 'scroll'); // 没问题
在TypeSctipt 3.1.4中 会报错
Argument of type 'HTMLElement | null' is not assignable to parameter of type 'HTMLElement'.
Type 'null' is not assignable to type 'HTMLElement'.

126 handleEvent(document.getElementById('hello'), 'scroll'); // 没问题
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
用的vscode1.29

null不能赋值给number类型变量

看到教程说,可以把null和undefined赋值给number类型的变量。然后我去试了一下,结果出现警告了,
不能将类型“null”分配给类型“number”。
我typescript的版本是3.6.3

拼写错误`enum.md`

手动赋值小节中

上面的例子中,递增到 3 的时候与前面的 Sun 的取值重复了,但是 TypeScript 并没有报错,导致 Days[3] 的值先是 "Sun",而后又被 "Web" 覆盖了。编译的结果是:

其中Web应该改为Wed

An in-range update of eslint-config-alloy is breaking the build 🚨

The devDependency eslint-config-alloy was updated from 2.0.0-alpha.3 to 2.0.0.

🚨 View failing branch.

This version is covered by your current version range and after updating it in your project the build failed.

eslint-config-alloy is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.

Status Details
  • continuous-integration/travis-ci/push: The Travis CI build failed (Details).

Commits

The new version differs by 5 commits.

See the full diff

FAQ and help

There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.


Your Greenkeeper Bot 🌴

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.