TypeScript

程式語言,編譯成JavaScript的JavaScript超集

TypeScript是由微軟進行開發和維護[4]的一種開源編程語言。TypeScript是JavaScript的嚴格語法超集,提供了可選的靜態型別檢查。

TypeScript
編程範型多范型函數式泛型指令式面向對象
設計者微軟
實作者微軟
面市時間2012年10月1日,​11年前​(2012-10-01[1]
當前版本
  • 5.4.2 (2024年3月6日;穩定版本)[2]
編輯維基數據鏈接
型態系統鴨子類型, 漸進類型英語Gradual typing, 結構類型英語Structural type system[3]
許可證Apache 許可證 2.0
文件擴展名.ts, .tsx
網站www.typescriptlang.org 編輯維基數據鏈接
啟發語言
C#JavaJavaScript
影響語言
AtScript英語AtScript、AssemblyScript

TypeScript的知名開發者有C#的首席架構師兼DelphiTurbo Pascal的創始人——安德斯·海爾斯伯格[5][6][7][8]

TypeScript是為開發大型應用程式而設計的,且可轉譯成JavaScript[9]。由於TypeScript是JavaScript的嚴格語法超集,因此任何現有的JavaScript程式都是合法的TypeScript程式。

TypeScript支援為現存JavaScript函式庫添加型別資訊的定義文件,方便其他程序像使用靜態型別的TypeScript實體一樣,使用現有程式庫中的值,就像是C++標頭檔可以描述目的檔(objectfile)的結構一樣。有許多第三方標頭檔為熱門函式庫像是jQueryMongoDBNode.jsD3.js等提供定義文件。

TypeScript編譯器本身也是用TypeScript編寫,並被轉譯為JavaScript,以Apache許可證第二版發布。

背景 編輯

經過微軟兩年的內部開發後,TypeScript於2012年10月首次發布(0.8版本)。[10][11]在發布後不久,Miguel de Icaza認可了這門語言,但批評了其糟糕的IDE支持性,僅有Microsoft Visual Studio IDE支持其代碼,但此IDE當時未在Linux和OS X操作系統上發布[12][13]。如今,在其他IDE中(例如:Eclipse),通過Palantir Technologies提供的插件支持,能支持TypeScript語法[14][15]。大部分主流的文本編輯器,例如:EmacsVimWebStormAtom[16]和微軟發布的Visual Studio Code也能支持TypeScript語法[17]

2013年發布的 TypeScript 0.9增加了對泛型的支持[18]。TypeScript 1.0在2014年的微軟開發者大會上發布。[19]Visual Studio 2013 Update 2為TypeScript提供了原生支持[20]

2014年7月,開發團隊發布了新的TypeScript編輯器,聲稱其性能提高了5倍。同時,代碼託管由CodePlex遷移至GitHub[21]

2016年9月22日,TypeScript 2.0發布,其中引入了幾個功能,例如開發者可以選擇不為變量分配空值等[22]

2018年7月30日,TypeScript 3.0發布[23],其中包含許多新功能,例如剩餘參數頁面存檔備份,存於網際網路檔案館)、展開語法頁面存檔備份,存於網際網路檔案館)、帶有元組的剩餘參數、帶有通用類型的剩餘參數等[24]

設計背景 編輯

TypeScript起源於JavaScript在微軟以及客戶中開發大型應用中遇到的缺點[25]。處理複雜JavaScript代碼帶來的挑戰使他們需要自定義工具來簡化組件開發流程[26]

TypeScript開發者尋求一種不破壞現有標準兼容性和跨平台支持的解決方案。直到ECMAScript標準為未來基於類編程提供支持後,Typescript開發便基於此方案。這形成了包含一組新的語法擴展的一個JavaScript編譯器,一個基於此提案的超集,可將TypeScript語法編譯為常規的JavaScript。從這個意義上來講,TypeScript是ECMAScript2015預期內容的預覽版本。提案中未包括的可選靜態類型被添加到了TypeScript中,有助於促進工具和IDE支持。

ECMAScript 6支持 編輯

TypeScript添加了ECMAScript 2015標準中定義的類、模塊和箭頭函數等語法的支持。

語言特性 編輯

TypeScript是一種為JavaScript添加特性的語言擴展。增加的功能包括:

以下功能是從ECMA 2015反向移植而來:

在語法上,TypeScript很類似JScript .NET,它是另外一個微軟對ECMA-262語言標準的實現,添加了對靜態類型、經典的面向對象語言特性(如類、繼承、接口和命名空間等)的支持。

類型批註 編輯

TypeScript通過類型批註提供靜態類型,以在編譯時啟動類型檢查。這是可選的,而且可以忽略而使用JavaScript常規的動態類型。

function add(left: number, right: number): number {
	return left + right;
}

對於基本類型的批註是numberbooleanstring。而弱型別或動態類型的結構則是any類型。

類型批註可以被導出到一個單獨的「聲明文件」,讓使用已被編譯為JavaScript類型的TypeScript腳本中的類型信息仍可用。批註可以為現有的JavaScript庫聲明,就像Node.js和jQuery所做的那樣。

沒有給出類型時,TypeScript編譯器會利用類型推斷來推斷類型。如果由於缺乏聲明而不能推斷出類型,那麼它的類型將默認為動態any類型。

聲明文件 編輯

當一個TypeScript腳本被編譯時,有一個產生作為編譯後的JavaScript的組件的一個接口而起作用的聲明文件(具有擴展名.d.ts)的選項。在這個過程中編譯器基本上帶走所有的函數和方法體而僅保留所導出類型的批註。當第三方開發者從TypeScript中使用它時,由此產生的聲明文件就可以被用於描述一個JavaScript庫或模塊導出的虛擬的TypeScript類型。

聲明文件的概念類似於CC++頭文件的概念。

declare module Arithmetics {
    export function add(left: number, right: number): number;
    export function subtract(left: number, right: number): number;
    export function multiply(left: number, right: number): number;
    export function divide(left: number, right: number): number;
}

可以為已存在的JavaScript庫(如jQuery和Node.js)撰寫類型聲明文件。

類別 編輯

TypeScript支持集成了可選的類別批註支持的ECMAScript6的類。

class Person {
    private name: string;
    private age: number;

    constructor(name: string, age: number){
        this.name = name;
        this.age = age;
    }
    
    toString(): string {
        return `${this.name}(${this.age})`;
    }
}

函式 編輯

TypeScript支援函式編程。以下為恆等函數的範例:

function id<T>(x: T): T {
    return x;
}

模組和命名空間 編輯

在TypeScript中,模組和命名空間是被區分開的。這兩項功能在TypeScript中皆支援將類型、介面、函數和變數封裝至容器內。命名空間(舊稱內部模組)使用JavaScript的立即呼叫函式表達式來封裝程式碼,而模組(舊稱外部模組)則是以JavaScript函式庫模式來達成(非同步模組定義CommonJS)。

與JavaScript的兼容性 編輯

TypeScript是JavaScript的超集。默認情況下編譯器以ECMA Script 3(ES3)為目標但ES5也是受支持的一個選項。一個TypeScript應用可以利用已存在的JavaScript腳本。編譯後的TypeScript腳本也可以在JavaScript中使用。

現有框架如jQuery和Node.js等受到完全支持。這些庫的類型聲明在源代碼中提供。

支持的瀏覽器和平台 編輯

運行於任何平台上的任何網頁瀏覽器都可以運行 TypeScript:[來源請求]由於它僅僅是被編譯為標準的JavaScript,一個腳本既可以被預編譯為JavaScript,也可以通過為TypeScript包含JavaScript編譯器實時編譯。

開發工具 編輯

編譯器 編輯

TypeScript編譯器,叫做tsc,本身也是用TypeScript寫成的。可以將TypeScript編譯為可以在任何JavaScript引擎(如瀏覽器)中執行的標準JavaScript。編譯器包也包含了腳本解釋器,用來執行編譯器。同時也有個Node.js包,在Node.js平台執行。

另外還有一個用JavaScript寫的alpha版本的客戶端編譯器,它在頁面載入時,實時執行TypeScript代碼。[28]

這種編譯器的當前版本默認支持ECMAScript 2015。一個選項是允許以ECMAScript 2015為目標,以利用該版本獨有的語言特性(比如生成器)。類是ECMAScript 2015標準的一部分,在這兩個模式下都可以使用。

IDE和編輯器支持 編輯

開源 編輯

TypeScript是開源的,其源代碼可以在Apache 2 License下從Github獲得。這個項目由Microsoft維護,但是任何人可以通過在Github項目頁發送反饋、提出建議和提交bugfixes而做出貢獻。[32]

已有一些批評提到,TypeScript鼓勵強類型,當前只有Microsoft Visual Studio支持在該語言上方便開發。最初的方案是在其它的編輯器上帶來強類型,IntelliSense英語IntelliSense代碼完成代碼重構的功能,但這可能不是一個簡單的任務。[33]此外,支持TypeScript開發的Visual Studio擴展不是開源的。最好的TypeScript開發體驗是在Microsoft Windows上,[34]然而隨着時間的流逝以及這種語言的開放性,加之編譯器自我託管,而且用TypeScript自身寫的,這很有可能會改變。通過編譯器的源代碼訪問到AST(抽象句法樹)以及詳細的語言規範文檔,社群已開始構建一個跨平台的編輯器,[35][36]利用和Visual Studio所用到的相同的語言服務以提供一個增強的編輯體驗。編輯器仍然在概念檢驗的階段,但已經運行於Linux、macOS和Windows,提供功能強度相同的IntelliSense、代碼完成和句法高亮方法。

參見 編輯

其它編譯為JavaScript的語言 編輯

參考資料 編輯

  1. ^ TypeScript. CodePlex. [2015-04-26]. (原始內容存檔於2015-04-03). 
  2. ^ Release TypeScript 5.4 microsoft/TypeScript. 2024年3月6日 [2024年3月19日]. 
  3. ^ Type Compatibility. TypeScript. [2018-03-21]. (原始內容存檔於2018-03-12). 
  4. ^ TypeScript GitHub官网. (原始內容存檔於2017-01-04). 
  5. ^ Foley, Mary Jo. Microsoft takes the wraps off TypeScript, a superset of JavaScript. ZDNet. CBS Interactive. 2012-10-01 [2015-04-26]. (原始內容存檔於2014-11-13). 
  6. ^ Somasegar, S. Somasegar’s blog. Somasegar’s blog. Microsoft. 2012-10-01 [2015-04-26]. (原始內容存檔於2015-04-22). 
  7. ^ Baxter-Reynolds, Matt. Microsoft TypeScript: Can the father of C# save us from the tyranny of JavaScript?. ZDNet. 2012-10-01 [2015-04-26]. (原始內容存檔於2014-08-03). 
  8. ^ Jackson, Joab. Microsoft Augments Javascript for Large-scale Development. CIO. IDG Enterprise. 2012-10-01 [2015-04-26]. (原始內容存檔於2013-12-17). 
  9. ^ Microsoft TypeScript: the JavaScript we need, or a solution looking for a problem? | Ars Technica. [2012-10-09]. (原始內容存檔於2015-03-29). 
  10. ^ Microsoft augments JavaScript for large-scale development. InfoWorld. IDG. 1 October 2012 [26 April 2015]. (原始內容存檔於2013-05-31). 
  11. ^ Turner, Jonathan. Announcing TypeScript 1.0. TypeScript Language team blog. Microsoft. 2 April 2014 [26 April 2015]. (原始內容存檔於2015-05-02). 
  12. ^ Microsoft TypeScript: Can the father of C# save us from the tyranny of JavaScript?. ZDNet. 1 October 2012 [12 October 2012]. (原始內容存檔於2014-08-03). And I think this is a pretty big misstep. If you're building web apps that run on anything other than Windows, you're likely using a Mac and most likely not using Visual Studio. You need the Visual Studio plug-in to get the IntelliSense. All you get without Visual Studio is the strong-typing. You don't get the productivity benefits you get from IntelliSense.. 
  13. ^ Miguel de Icaza. TypeScript: First Impressions. 1 October 2012 [12 October 2012]. (原始內容存檔於2019-02-24). But TypeScript only delivers half of the value in using a strongly typed language to Unix developers: strong typing. Intellisense, code completion and refactoring are tools that are only available to Visual Studio Professional users on Windows. There is no Eclipse, MonoDevelop or Emacs support for any of the language features 
  14. ^ TypeScript-Unterstützung für Eclipse. heise Developer. 6 August 2013 [26 April 2015]. (原始內容存檔於2018-07-11). 
  15. ^ TypeScript. Eclipse Marketplace. Eclipse Foundation. [26 April 2015]. (原始內容存檔於2018-10-10). 
  16. ^ TypeStrong: The only TypeScript package you will ever need. [21 July 2016]. (原始內容存檔於2018-12-19). 
  17. ^ Hillar, Gastón. Working with TypeScript in Visual Studio 2012. Dr. Dobb's Journal. 14 May 2013 [26 April 2015]. (原始內容存檔於2018-09-29). 
  18. ^ TypeScript 0.9 arrives with new compiler, support for generics. The Register. 18 June 2013 [26 April 2015]. (原始內容存檔於2018-03-11). 
  19. ^ Hejlsberg, Anders. TypeScript. Channel 9. Microsoft. 2 April 2014 [26 April 2015]. (原始內容存檔於2015-05-25). 
  20. ^ Jackson, Joab. Microsoft TypeScript graduates to Visual Studio. PC World. IDG. 25 February 2014 [26 April 2015]. (原始內容存檔於2016-03-11). 
  21. ^ Turner, Jonathan. New Compiler and Moving to GitHub. TypeScript Language team blog. Microsoft. 21 July 2014 [26 April 2015]. (原始內容存檔於2015-10-27). 
  22. ^ Bright, Peter. TypeScript, Microsoft's JavaScript for big applications, reaches version 2.0. Ars Technica. Condé Nast. 22 September 2016 [22 September 2016]. (原始內容存檔於2018-12-21). 
  23. ^ Announcing TypeScript 3.0. 30 July 2018 [16 March 2020]. (原始內容存檔於2020-05-30). 
  24. ^ TypeScript 3.0. 30 July 2018 [16 March 2020]. (原始內容存檔於2020-06-06). 
  25. ^ Anders Hejlsberg. What is TypeScript and why with Anders Hejlsberg. www.hanselminutes.com. 5 October 2012 [15 January 2014]. (原始內容存檔於2018-12-27). 
  26. ^ S. Somasegar. TypeScript: JavaScript Development at Application Scale. msdn.com. 1 October 2012 [27 November 2013]. (原始內容存檔於2015-04-22). 
  27. ^ 存档副本. [2012-11-10]. (原始內容存檔於2012-11-13). 
  28. ^ niutech/typescript-compile. GitHub. [2015-04-26]. (原始內容存檔於2018-06-11). 
  29. ^ Olivier Bloch. Sublime Text, Vi, Emacs: TypeScript enabled!. Microsoft. 2012-10-01 [2012-10-28]. (原始內容存檔於2012-10-29). 
  30. ^ TypeScript support in WebStorm 6. JetBrains. [2016-12-17]. (原始內容存檔於2016-06-02). 
  31. ^ TypeScript support in ReSharper 8.1. JetBrains. [2016-12-17]. (原始內容存檔於2014-02-02). 
  32. ^ 存档副本. [2016-12-17]. (原始內容存檔於2017-01-04). 
  33. ^ Miguel de Icaza. TypeScript: First Impressions. 2012-10-01 [2012-10-12]. (原始內容存檔於2019-02-24). But TypeScript only delivers half of the value in using a strongly typed language to Unix developers: strong typing. Intellisense, code completion and refactoring are tools that are only available to Visual Studio Professional users on Windows. There is no Eclipse, MonoDevelop or Emacs support for any of the language features 
  34. ^ Microsoft TypeScript: Can the father of C# save us from the tyranny of JavaScript?. ZDNet. 2012-10-01 [2012-10-12]. (原始內容存檔於2014-08-03). And I think this is a pretty big misstep. If you're building web apps that run on anything other than Windows, you're likely using a Mac and most likely not using Visual Studio. You need the Visual Studio plug-in to get the IntelliSense. All you get without Visual Studio is the strong-typing. You don't get the productivity benefits you get from IntelliSense.. 
  35. ^ Code Assistant for TypeScript. [2013-01-07]. (原始內容存檔於2013-01-21). 
  36. ^ TypeScript Editor. 2012-11-21 [2013-01-07]. (原始內容存檔於2013-02-18). 

外部連結 編輯