typescript 學習

typescript將在不久的將來從前端大一統的趨勢中脫穎而出成為主流編譯器。學習ts對前端開發人員來說是不可或缺的。同時,也要抓緊學習es2015/6/7。ts和es6并不是對立的。而是相輔相成的。ts的競爭和打擊對象實質上是babel……

官方資料

?# 官方地址:
?https://www.tslang.cn

?# github:
?https://github.com/Microsoft/TypeScript

?# 官方文檔
?http://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-2.html

安裝與編譯:

# 安裝
npm install -g typescript
# 編譯
tsc helloworld.ts

?

demo1:支持Commonjs規范

# helloworld.ts
import app from './app'
console.log(app)# app.ts
export default {sayHello () {console.log("Hello World!!")}
}

執行tsc helloworld.ts

控制臺執行:node helloworld

控制臺輸出:Hello World!!

?

?

demo2: 快速掃基礎篇

# 基礎類型
let isDone: boolean = false;
let decLiteral: number = 6;
let name: string = "bob";# 模板字符串 和 內嵌表達式
let name: string = `Gene`;
let age: number = 37;
let sentence: string = `Hello, my name is ${ name }.I'll be ${ age + 1 } years old next month.`;

# 數組 與 泛型數組
let list: number[] = [1, 2, 3];
let list: Array<number> = [1, 2, 3];# 元組
let x: [string, number];
x = ['hello', 10]; // OK
x = [10, 'hello']; // Error
console.log(x[0].substr(1)); // OK
console.log(x[1].substr(1)); // Error, 'number' does not have 'substr'
x[3] = 'world'; // OK
onsole.log(x[1].toString()); // OK,?'string' 和 'number' 都有 toString
x[6] = true; // Error, 布爾不是(string | number)類型

?

demo3: 快速掃基礎篇2

# 枚舉
// 默認情況下,從 0 開始為元素編號
enum Color {Red, Green, Blue};
let c: Color = Color.Green; // 1enum Color {Red = 1, Green, Blue};
let c: Color = Color.Green; // 2enum Color {Red = 1, Green = 2, Blue = 4};
let c: Color = Color.Green; // 2//查找相應的名字
enum Color {Red = 1, Green, Blue};
let colorName: string = Color[2];
console.log(colorName) // Green

# 任意值
let notSure: any = 4;
notSure = "maybe a string instead";
notSure = false; // okay, definitely a boolean

let list: any[] = [1, true, "free"];
list[1] = 100;# void
function warnUser(): void {alert("This is my warning message");
}

?

demo4 : ts的核心之一 接口

# 接口初探
function printLabel(labelledObj: { label: string }) {console.log(labelledObj.label);
}
let myObj = { size: 10, label: "Size 10 Object" };
printLabel(myObj);  // Size 10 Object

類型檢查器會查看 printLabel 的調用。 printLabel 有一個參數,并要求這個對象參數有一個名為 label 類型
為 string 的屬性。 需要注意的是,我們傳入的對象參數實際上會包含很多屬性,但是編譯器只會檢查那些必需
的屬性是否存在,并且其類型是否匹配。// 將接口單獨抽離出來
interface LabelledValue {label: string;
}
function printLabel(labelledObj: LabelledValue) {console.log(labelledObj.label);
}
let myObj = {size: 10, label: "Size 10 Object"};
printLabel(myObj);類型檢查器不會去檢查屬性的順序,只要相應的屬性存在并且類型也是對的就可以。# 接口可選屬性
interface SquareConfig {color?: string;width?: number;
}function createSquare(config: SquareConfig): {color: string; area: number} {let newSquare = {color: "white", area: 100};if (config.color) {newSquare.color = config.color;}if (config.width) {newSquare.area = config.width * config.width;}return newSquare;
}
let mySquare = createSquare({color: "black"}); // { color: 'black', area: 100 }

?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541631.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541631.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541631.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

計算機中央處理器cpu_中央處理器(CPU)| 計算機科學組織

計算機中央處理器cpu中央處理器(CPU) (Central Processing Unit (CPU)) The CPU is the brain of the computer system. It works as an administrator of a system. CPU是計算機系統的大腦。 它以系統管理員的身份工作。 All the operations within the system are supervised…

計算機應用基礎專2020春,計算機應用基礎(專)(專,2020春)(20200831130023).pdf

計算機應用基礎(專)(專&#xff0c; 2020 春)使用 " 格式刷”按鈕&#xff0c;可以進行 ___________操作。選擇一項&#xff1a;a. 復制文本的格式b. 刪除文本c. 復制文本d. 保持文本你的回答正確正確答案是&#xff1a;復制文本的格式在 Word 的文檔中插入復雜的數學公式…

computed set 自定義參數_深入理解vmodel之自定義組件用法

根據上一篇《深入理解 v-model 之表單用法》基本對 v-model 有了比較深的理解&#xff0c;接下來我們看看它如何在自定義組件中使用。首先&#xff0c;我們知道下面兩個用法等價的&#xff1a;<input v-model"msg" /><input :value"msg" input&qu…

01json轉字符串

/** * json轉字符串聲明 */ (NSString *)jsonToString:(NSDictionary *)dic; /** * json轉字符串實現 */ (NSString *)jsonToString:(NSDictionary *)dic { if(!dic){ return nil; } NSData *jsonData [NSJSONSerialization dataWithJSONObject:dic options:NSJSONWriting…

AYUSH的完整形式是什么?

AYUSH&#xff1a;阿育吠陀&#xff0c;瑜伽和自然療法&#xff0c;烏納尼&#xff0c;悉達多和順勢療法 (AYUSH: Ayurvedic, Yoga and Naturopathy, Unani, Siddha and Homeopathy) AYUSH is an abbreviation of Ayurvedic, Yoga and Naturopathy, Unani, Siddha, and Homeopa…

大班科學電子計算機,計算器教案

計算器的認識和簡單應用教學內容&#xff1a;義務教育六年制小學第九冊第二單元第42頁。教學目標&#xff1a;1、通過學生自主探究&#xff0c;掌握計算器的使用方法&#xff0c;并能夠用計算器進行簡單的計算。2、借助計算器解決生活中的數學問題、探索數學規律&#xff0c;體…

arraylist能否接收強轉類型_ArrayList 源碼解析

點擊上方"IT牧場"&#xff0c;選擇"設為星標"技術干貨每日送達&#xff01;前言 JDK源碼解析系列文章&#xff0c;都是基于JDK8分析的&#xff0c;雖然JDK14已經出來&#xff0c;但是JDK8我還不會&#xff0c;我…類圖 實現了RandomAccess接口&#xff0c;…

exit c+_C / C ++中的exit(0)vs exit(1)與示例

exit cexit() is a library function in C/C programming language, it is used to terminate the calling process (function) immediately i.e. we can say it is used for the normal program termination and also perform the several cleanup steps. exit()是C / C 編程語…

校園計算機網絡系統,校園計算機網絡系統

一、校園網的基本概念基本功能&#xff1a;數據交換、資源共享&#xff0c;這里所指的數據包括各種信息&#xff0c;基本組成和結構&#xff1a;基本網絡由網絡網絡的分類&#xff1a;有多種分類方法&#xff0c;按規模可分為局域網、區域網、&127;廣域網。局域網服務范圍一…

mc有什么紅石機器人_我的世界10月考試!來測測你的MC成績吧~

考試規則&#xff1a;本次考試為閉卷考試&#xff0c;考生需要在30分鐘內完成試卷。試卷總分為100分&#xff0c;其中包括單項選擇題50分&#xff0c;多項選擇題20分&#xff0c;判斷題30分。考試內容包括《我的世界》手游1.11.0及以上版本在不添加任何模組的情況下的所有游戲內…

130、 Android OkHttp完全解析(轉載)

完全解析&#xff1a;http://blog.csdn.net/lmj623565791/article/details/47911083 從原理角度解析http文件上傳 http://blog.csdn.net/lmj623565791/article/details/23781773 https://github.com/hongyangAndroid/okhttputils

json轉string示例_C.示例中的String.Copy()方法

json轉string示例C&#xff03;String.Copy()方法 (C# String.Copy() Method) String.Copy() method is used to copy a string to new string object, it returns a new instance of String with the same value as given string. String.Copy()方法用于將字符串復制到新的字符…

自定義分頁 html,MVC 自定義HtmlHelper幫助類型之分頁

方法一&#xff1a;在項目中增加App_Code文件夾&#xff0c;新增一個MyHtmlper.cshtml視圖文件寫入代碼&#xff1a;helper Pagger(int pageIndex, int pageCount){for (int i 1; i < pageCount; i){if (i ! pageIndex){(i)}else{i}}}新增一個HomeControllerpublic class H…

vue 對象繼承_Vue2.0中組件的繼承與擴展是什么

Vue2.0中組件的繼承與擴展是什么發布時間&#xff1a;2020-12-07 14:04:09來源&#xff1a;億速云閱讀&#xff1a;100作者&#xff1a;小新小編給大家分享一下Vue2.0中組件的繼承與擴展是什么&#xff0c;相信大部分人都還不怎么了解&#xff0c;因此分享這篇文章給大家參考一…

stack示例_C.示例中的Stack.Clone()方法

stack示例C&#xff03;Stack.Clone()方法 (C# Stack.Clone() method) Stack.Clone() method is used to create a shallow copy of the stack. Stack.Clone()方法用于創建堆棧的淺表副本。 Syntax: 句法&#xff1a; Object Stack.Clone();Parameters: None 參數&#xff1a…

簡述計算機圖形的圖形應用主要有哪些,5計算機圖形學考試簡答題復習.doc

5計算機圖形學考試簡答題復習計算機圖形學考試簡答題復習1、簡述計算機動畫的概念&#xff0c;它經歷了哪幾個階段的發展&#xff1f;(2分)計算機動畫是指采用圖形與圖像的處理技術&#xff0c;借助于編程或動畫制作軟件生成一系列的景物畫面&#xff0c;其中當前幀是前一幀的部…

在圖片中選定任意凸多邊形制作掩膜程序MATLAB

function S get_convex_S(C,vx,vy) %該函數實現的功能為指定圖像中多邊形的頂點&#xff0c;返回屬于該凸多邊形中的所有像素點 %xv&#xff0c;yv為頂點坐標按照順時針或者逆時針。vx(1) xv(end); yv(1) yv(end) %輸入的C是結構&#xff0c;vx vy是數組存的是頂點坐標。 %輸…

js console 輸出到文件_Node.js核心入門

正文核心模塊是Node.js的心臟&#xff0c;主要是有一些精簡高效的庫組成(這方面和Python有很大的相似之處)&#xff0c;為Node.js提供了基礎的API。主要內容包括&#xff1a;Node.js核心入門(一)全局對象常用工具事件機制Node.js核心入門(二)文件系統訪問HTTP服務器與客戶端全局…

scala 當前日期_如何在Scala中檢查當前日期和時間?

scala 當前日期Scala is a general-purpose programming language, which is majorly used for data manipulation. It has libraries and support for almost all different utilities that are important. One of the important things that are required while programming …

計算機科學考試大綱,計算機科學與技術考試大綱.doc

計算機科學與技術考試大綱計算機科學與技術專業本專業的專業課程考試為“計算機軟件基礎”和“計算機硬件基礎”兩門課程的組合試卷&#xff0c;卷面總分200分&#xff0c;時間150分鐘&#xff0c;考試方式為筆試。考試可攜帶計數器&#xff0c;但禁止攜帶文曲星、商務通等帶有…