Create a new project

Create a new project Choose File Choose New Project (Ctrl + Shift + N) Choose Visual C# Choose ASP.NET Web Application Choose ASP.NET 5 Empty Let’s uncheck “Host in the cloud” since we’re going to run this locally. Run the application and make sure that it works.

Create a gulpfile.js

Create a gulpfile.js In the project root, create the file gulpfile.js: var gulp = require("gulp"); var ts = require("gulp-typescript"); var tsProject = ts.createProject("tsconfig.json"); gulp.task("default", function () { return tsProject.src() .pipe(ts(tsProject)) .js.pipe(gulp.dest("dist")); });

Contextual Type

Contextual Type Type inference also works in “the other direction” in some cases in TypeScript. This is known as “contextual typing”. Contextual typing occurs when the type of an expression is implied by its location. For example: window.onmousedown = function(mouseEvent) { console.log(mouseEvent.buton); //<- Error }; For the code above to give the type error, the TypeScript type checker used the type of the Window.onmousedown function to infer the type of the function expression on the

Consuming Dependencies

Consuming Dependencies There are several kinds of dependencies you might have.

Consuming

Consuming From there you’ll be able to use lodash in your TypeScript code with no fuss. This works for both modules and global code. For example, once you’ve npm install-ed your type declarations, you can use imports and write import * as _ from "lodash"; _.padStart("Hello TypeScript!", 20, " "); or if you’re not using modules, you can just use the global variable _. _.padStart("Hello TypeScript!", 20, " ");

Constructor functions

Constructor functions When you declare a class in TypeScript, you are actually creating multiple declarations at the same time. The first is the type of the instance of the class. class Greeter { greeting: string; constructor(message: string) { this.greeting = message; } greet() { return "Hello, " + this.greeting; } } let greeter: Greeter; greeter = new Greeter("world"); console.log(greeter.greet()); Here, when we say let greeter: Greeter, we’re using Greeter as the type of

Constraints

Generic Constraints If you remember from an earlier example, you may sometimes want to write a generic function that works on a set of types where you have some knowledge about what capabilities that set of types will have. In our loggingIdentity example, we wanted to be able to access the .length property of arg, but the compiler could not prove that every type had a .length property, so it warns us that we can’t make this assumption. function loggingIdentity<T>(arg: T): T { console.lo

const declarations

const declarations const declarations are another way of declaring variables. const numLivesForCat = 9; They are like let declarations but, as their name implies, their value cannot be changed once they are bound. In other words, they have the same scoping rules as let, but you can’t re-assign to them. This should not be confused with the idea that the values they refer to are immutable. const numLivesForCat = 9; const kitty = { name: "Aurora", numLives: numLivesForCat, } // Error kitty

Composition

Decorator Composition Multiple decorators can be applied to a declaration, as in the following examples: On a single line: @f @g x On multiple lines: @f @g x When multiple decorators apply to a single declaration, their evaluation is similar to function composition in mathematics. In this model, when composing functions f and g, the resulting composite (f ∘ g)(x) is equivalent to f(g(x)). As such, the following steps are performed when evaluating multiple decorators on a single declarat

Compiler Options

Compiler Options Option Type Default Description --allowJs boolean true Allow JavaScript files to be compiled. --allowSyntheticDefaultImports boolean module === "system" Allow default imports from modules with no default export. This does not affect code emit, just typechecking. --allowUnreachableCode boolean false Do not report errors on unreachable code. --allowUnusedLabels boolean false Do not report errors on unused labels. --baseUrl string Base directory to resolve non-relative module na