TypeScript: Difference between revisions

 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
TypeScript is a typed version of JavaScript. It claims to be a superset of JavaScript.
TypeScript is a typed version of JavaScript. It claims to be a superset of JavaScript but most JS code will need modifications to compile properly as TS code. 
The syntax is very similar to Kotlin.


==Syntax==
==Syntax==
Here we focus on the syntax differences between TypeScript and JavaScript.
Here we focus on the syntax differences between TypeScript and JavaScript. See [[JavaScript]] for JS syntax.


===Classes===
===Classes===
Line 19: Line 20:
}
}
</syntaxhighlight>
</syntaxhighlight>
===Functions===
====Overloads====
See [https://www.typescriptlang.org/docs/handbook/functions.html#overloads Overloads]
TypeScript does not support overloading in the traditional sense, with multiple implementations. 
However you can have multiple function declarations similar to prototypes.


===Interfaces===
===Interfaces===
[https://www.typescriptlang.org/docs/handbook/interfaces.html Interfaces]
[https://www.typescriptlang.org/docs/handbook/interfaces.html Interfaces]


Unlike JavaScript, TypeScript comes with support for interfaces:
Unlike JavaScript, TypeScript comes with support for interfaces.<br>
You can treat these interfaces as types or structs.
<syntaxhighlight lang="typescript">
<syntaxhighlight lang="typescript">
interface LabeledValue {
interface LabeledValue {
Line 29: Line 38:
}
}
</syntaxhighlight>
</syntaxhighlight>
===Generics===
[https://www.typescriptlang.org/docs/handbook/generics.html Generics]
===Type Guards===
[https://www.typescriptlang.org/docs/handbook/advanced-types.html Advanced Types]


==Compilation==
==Compilation==
Line 40: Line 55:
#:<pre>npm install -g typescript</pre>
#:<pre>npm install -g typescript</pre>
# Compile your typescript file
# Compile your typescript file
#:<pre>tsc greeter.js</pre>
#:<pre>tsc greeter.ts</pre>


===webpack===
===webpack===
Line 50: Line 65:
==Resources==
==Resources==
* [https://www.typescriptlang.org/docs/handbook/intro.html The TypeScript Handbook]
* [https://www.typescriptlang.org/docs/handbook/intro.html The TypeScript Handbook]
[[Category:Programming languages]]