typescript-tutorial
typescript-tutorial copied to clipboard
TypeScript 入门教程
``` > let j: void = null [eval].ts:3:5 - error TS2322: Type 'null' is not assignable to type 'void'. ```
## 混合类型 [之前学习过](../basics/type-of-function.md#接口中函数的定义),可以使用接口的方式来定义一个函数需要符合的形状: ```ts interface SearchFunc { (source: string, subString: string): boolean; } let mySearch: SearchFunc; mySearch = function(source: string, subString: string) { return source.search(subString) !== -1; } ``` 有时候,一个函数还可以有自己的属性和方法:...
“TypeScript 虽然有官方手册及其非官方中文版,但是它每一章都希望能详尽的描述一个概念,导致前面的章节就会包含很多后面才会学习到的内容,而有些本该一开始就了解的基础知识却在后面才会涉及。如果是初学者,可能需要阅读多次才能理解。所以它更适合用来查阅,而不是学习。”这一段,我觉得不成立。**因为官网包含quick start或者tutorials这种快速入门类文档,也包括reference或者handbook这种供查阅用的手册。设置还包含处于中间位置的guideline文档。**
开启保存时自动修复的功能: ```json { "eslint.autoFixOnSave": true, "eslint.validate": [ "javascript", "javascriptreact", { "language": "typescript", "autoFix": true }, ], "typescript.tsdk": "node_modules/typescript/lib" } ``` vscode现在要改成: ```json { "eslint.validate": [ "javascript", "javascriptreact", "typescript" ], "typescript.tsdk":...
``` let tom: [string, number]; tom[0] = 'Tom'; tom[1] = 25; tom[0].slice(1); tom[1].toFixed(2); ``` TSError: ⨯ Unable to compile TypeScript: tom is used before being assigned
``` type EventNames = 'click' | 'scroll' | 'mousemove'; function handleEvent(ele: Element, event: EventNames) { // do something } handleEvent(document.getElementById('hello'), 'scroll'); // 没问题 handleEvent(document.getElementById('world'), 'dbclick'); // 报错,event 不能为 'dbclick' //...
let anyThing: any = "any"; console.log(anyThing.myName); // console.log(anyThing.myName.firstName); // anyThing.setName("Jerry"); // anyThing.setName("Jerry").sayHello(); // anyThing.myName.setFirshName("Cat"); 后四句都会报错
https://ts.xcatliu.com/basics/declaration-files#di-san-fang-sheng-ming-wen-jian
这个说法应该有问题: 应该是:变量如果在声明的时候,未指定其类型也没有赋值,那么它会被识别为任意值类型 你说的情况可以类型推断出来,所以也会报错