<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=TypeScript_2</id>
	<title>TypeScript 2 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=TypeScript_2"/>
	<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=TypeScript_2&amp;action=history"/>
	<updated>2026-04-21T09:01:45Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://training-course-material.com/index.php?title=TypeScript_2&amp;diff=63420&amp;oldid=prev</id>
		<title>Wanghao: /* Understanding Typing, Variables, and Functions ⌘ */</title>
		<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=TypeScript_2&amp;diff=63420&amp;oldid=prev"/>
		<updated>2017-11-02T12:25:57Z</updated>

		<summary type="html">&lt;p&gt;&lt;span class=&quot;autocomment&quot;&gt;Understanding Typing, Variables, and Functions ⌘&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;[[Category:private]]&lt;br /&gt;
&amp;lt;slideshow style=&amp;quot;nobleprog&amp;quot; headingmark=&amp;quot;⌘&amp;quot; incmark=&amp;quot;…&amp;quot; scaled=&amp;quot;true&amp;quot; font=&amp;quot;Trebuchet MS&amp;quot; &amp;gt;&lt;br /&gt;
;title: TypeScript 2&lt;br /&gt;
;author: Hao Wang&lt;br /&gt;
&amp;lt;/slideshow&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Introduction ⌘==&lt;br /&gt;
&lt;br /&gt;
*ECMAScript2015/ES2015/ES6&lt;br /&gt;
 &amp;lt;big&amp;gt;node --v8-options | grep &amp;quot;in progress&amp;quot;&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Installing TypeScript ⌘==&lt;br /&gt;
&lt;br /&gt;
== Preparing a TypeScript project ⌘==&lt;br /&gt;
&lt;br /&gt;
== Understanding Typing, Variables, and Functions ⌘==&lt;br /&gt;
== Basic Types ⌘==&lt;br /&gt;
=== Basic Types - boolean ⌘===&lt;br /&gt;
* let isDone: boolean = false;&lt;br /&gt;
=== Basic Types - number ⌘===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
let decimal: number = 6;&lt;br /&gt;
let hex: number = 0xf00d;&lt;br /&gt;
let binary: number = 0b1010;&lt;br /&gt;
let octal: number = 0o744;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Basic Types - string ⌘===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
let color: string = &amp;quot;blue&amp;quot;;&lt;br /&gt;
color = &amp;#039;red&amp;#039;;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Basic Types - string - use template string ⌘===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
`let fullName: string = `Bob Bobbington`;&lt;br /&gt;
let age: number = 37;&lt;br /&gt;
let sentence: string = `Hello, my name is ${ fullName }.&lt;br /&gt;
&lt;br /&gt;
I&amp;#039;ll be ${ age + 1 } years old next month.`;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Basic Types - Array ⌘===&lt;br /&gt;
* let list: number[] = [1, 2, 3];&lt;br /&gt;
* let list: Array&amp;lt;number&amp;gt; = [1, 2, 3];&lt;br /&gt;
&lt;br /&gt;
=== Basic Types - Enum ⌘===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Declare a tuple type&lt;br /&gt;
let x: [string, number];&lt;br /&gt;
// Initialize it&lt;br /&gt;
x = [&amp;quot;hello&amp;quot;, 10]; // OK&lt;br /&gt;
// Initialize it incorrectly&lt;br /&gt;
x = [10, &amp;quot;hello&amp;quot;]; // Error&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Basic Types - any ⌘===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
let notSure: any = 4;&lt;br /&gt;
notSure = &amp;quot;maybe a string instead&amp;quot;;&lt;br /&gt;
notSure = false; // okay, definitely a boolean&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Basic Types - Void ⌘===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
function warnUser(): void {&lt;br /&gt;
    alert(&amp;quot;This is my warning message&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
=== Basic Types - undefnined and null ⌘===&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
/ Not much else we can assign to these variables!&lt;br /&gt;
let u: undefined = undefined;&lt;br /&gt;
let n: null = null;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* assign themselves as value if use for typing&lt;br /&gt;
* subtype of all other type so you can assign them to other tpyes&lt;br /&gt;
* use -- strictNullCheck &lt;br /&gt;
** to only allow assigning to &amp;#039;&amp;#039;&amp;#039;void&amp;#039;&amp;#039;&amp;#039; type and themselves. &lt;br /&gt;
** const text: string = null;&lt;br /&gt;
** using union to make typing explicit. string | null | undefined&lt;br /&gt;
&lt;br /&gt;
=== Basic Types - never ⌘===&lt;br /&gt;
* make sure &amp;#039;&amp;#039;&amp;#039;no return&amp;#039;&amp;#039;&amp;#039;. Not even a void type.&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
// Function returning never must have unreachable end point&lt;br /&gt;
function error(message: string): never {&lt;br /&gt;
    throw new Error(message);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Inferred return type is never&lt;br /&gt;
function fail() {&lt;br /&gt;
    return error(&amp;quot;Something failed&amp;quot;);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// Function returning never must have unreachable end pointnot &lt;br /&gt;
function infiniteLoop(): never {&lt;br /&gt;
    while (true) {&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Type Assertion ⌘===&lt;br /&gt;
* When you know the type better than TypeScript&lt;br /&gt;
* like type casting&lt;br /&gt;
* not type casting - there is not type checking or restructuring&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//angel brackets  &amp;lt;--- not for JSX&lt;br /&gt;
let someValue: any = &amp;quot;this is a string&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
let strLength: number = (&amp;lt;string&amp;gt;someValue).length;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
//as&lt;br /&gt;
let someValue: any = &amp;quot;this is a string&amp;quot;;&lt;br /&gt;
&lt;br /&gt;
let strLength: number = (someValue as string).length;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Working with Interfaces and Classes ⌘==&lt;br /&gt;
&lt;br /&gt;
=== Interfaces ⌘===&lt;br /&gt;
* Type-checking focuses on the &amp;#039;&amp;#039;&amp;#039;shape&amp;#039;&amp;#039;&amp;#039; that values have. This is sometimes called &amp;#039;&amp;#039;&amp;#039;“duck typing”&amp;#039;&amp;#039;&amp;#039; or “structural subtyping”. &amp;#039;&amp;#039;If it sounds like a duck, walks a duck, it is a duck&amp;#039;&amp;#039;&lt;br /&gt;
* In TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well as contracts with code outside of your project.&lt;br /&gt;
&lt;br /&gt;
=== Classes ⌘===&lt;br /&gt;
* Starting with ECMAScript 2015, also known as ECMAScript 6, JavaScript programmers will be able to build their applications using this object-oriented class-based approach. In TypeScript, we allow developers to use these techniques now, and compile them down to JavaScript that works across all major browsers and platforms, without having to wait for the next version of JavaScript.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==== Parameter Properties ⌘====&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
class Octopus {&lt;br /&gt;
    readonly name: string;&lt;br /&gt;
    readonly numberOfLegs: number = 8;&lt;br /&gt;
    constructor (theName: string) {&lt;br /&gt;
        this.name = theName;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
class Octopus {&lt;br /&gt;
    readonly numberOfLegs: number = 8;&lt;br /&gt;
    constructor(readonly name: string) {&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Organizing your code with Namespaces ⌘==&lt;br /&gt;
&lt;br /&gt;
=== A note ⌘===&lt;br /&gt;
* Internal modules” are now “namespaces”. “External modules” are now simply “modules”, as to align with ECMAScript 2015’s terminology, (namely that &amp;#039;&amp;#039;&amp;#039;module X {&amp;#039;&amp;#039;&amp;#039; is equivalent to the now-preferred &amp;#039;&amp;#039;&amp;#039;namespace X {&amp;#039;&amp;#039;&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=== Working with other JS libraries ⌘===&lt;br /&gt;
* type definition is required (*.d.ts)&lt;br /&gt;
* https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/jquery&lt;br /&gt;
* npm install --save-dev @types/jquery@1&lt;br /&gt;
&lt;br /&gt;
== Reusing code through Modules ⌘==&lt;br /&gt;
&lt;br /&gt;
=== What is a Module ⌘===&lt;br /&gt;
* Modules are executed within their own scope, not in the global scope; &lt;br /&gt;
* To consume a variable, function, class, interface, etc. exported from a different module, it has to be imported&lt;br /&gt;
* Modules are declarative; the relationships between modules are specified in terms of imports and exports at the file level.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
*In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module.&lt;br /&gt;
*A script is a file with no imports or exports.&lt;br /&gt;
&lt;br /&gt;
=== Module loading ⌘===&lt;br /&gt;
* Module Definitions and Module Loaders:&lt;br /&gt;
** CommonJS - webpack&lt;br /&gt;
** AMD - Require.js, browserify &lt;br /&gt;
** ECMAScript 2015 native modules (ES6) &lt;br /&gt;
** CommonJS/Webpack/ES6 - SystemJS&lt;br /&gt;
&lt;br /&gt;
* SimpleModule.ts&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
import m = require(&amp;quot;mod&amp;quot;);&lt;br /&gt;
export let t = m.something + 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* AMD / RequireJS SimpleModule.js&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
define([&amp;quot;require&amp;quot;, &amp;quot;exports&amp;quot;, &amp;quot;./mod&amp;quot;], function (require, exports, mod_1) {&lt;br /&gt;
    exports.t = mod_1.something + 1;&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
* CommonJS / Node SimpleModule.js&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
var mod_1 = require(&amp;quot;./mod&amp;quot;);&lt;br /&gt;
exports.t = mod_1.something + 1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Working with Other JavaScript Libraries ⌘===&lt;br /&gt;
*In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module.&lt;br /&gt;
&lt;br /&gt;
== Compiling, testing and running TypeScript ⌘==&lt;br /&gt;
&lt;br /&gt;
== Debugging TypeScript ⌘==&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;big&amp;gt;node --inspect dist/&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
 &amp;lt;big&amp;gt;chrome://inspect&amp;lt;/big&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Launching your application ⌘==&lt;br /&gt;
&lt;br /&gt;
== Closing remarks ⌘==&lt;br /&gt;
&lt;br /&gt;
* See [https://www.mediawiki.org/wiki/Extension:S5SlideShow#Getting_started here] what else you may want to customize&lt;/div&gt;</summary>
		<author><name>Wanghao</name></author>
	</entry>
</feed>