String Literal Types

String Literal Types String literal types allow you to specify the exact value a string must have. In practice string literal types combine nicely with union types, type guards, and type aliases. You can use these features together to get enum-like behavior with strings. type Easing = "ease-in" | "ease-out" | "ease-in-out"; class UIElement { animate(dx: number, dy: number, easing: Easing) { if (easing === "ease-in") { // ... } else if (easing === "ease-out") { } else

String

String Another fundamental part of creating programs in JavaScript for webpages and servers alike is working with textual data. As in other languages, we use the type string to refer to these textual datatypes. Just like JavaScript, TypeScript also uses double quotes (") or single quotes (') to surround string data. let color: string = "blue"; color = 'red'; You can also use template strings, which can span multiple lines and have embedded expressions. These strings are surrounded by the backt

Strategies

Module Resolution Strategies There are two possible module resolution strategies: Node and Classic. You can use the --moduleResolution flag to specify the module resolution strategy. The default if not specified is Node.

Static Properties

Static Properties Up to this point, we’ve only talked about the instance members of the class, those that show up on the object when it’s instantiated. We can also create static members of a class, those that are visible on the class itself rather than on the instances. In this example, we use static on the origin, as it’s a general value for all grids. Each instance accesses this value through prepending the name of the class. Similarly to prepending this. in front of instance accesses, here w

Splitting Across Files

Splitting Across Files As our application grows, we’ll want to split the code across multiple files to make it easier to maintain.

split

Symbol.split A regular expression method that splits a string at the indices that match the regular expression. Called by the String.prototype.split method.

species

Symbol.species A function valued property that is the constructor function that is used to create derived objects.

Setting up your Directories

Setting up your Directories If you’re writing in plain JavaScript, it’s likely that you’re running your JavaScript directly, where your .js files in a src, lib, or dist directory, and then ran as desired. If that’s the case, the files that you’ve written are going to used as inputs to TypeScript, and you’ll run the outputs it produces. During our JS to TS migration, we’ll need to separate our input files to prevent TypeScript from overwriting them. If your output files need to reside in a speci

Set up the server

Set up the server In project.json add another entry in "dependencies": "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final" The resulting dependencies should look like this: "dependencies": { "Microsoft.AspNet.IISPlatformHandler": "1.0.0-rc1-final", "Microsoft.AspNet.Server.Kestrel": "1.0.0-rc1-final", "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final" }, Replace the body of Configure in Startup.cs with public void Configure(IApplicationBuilder app) { app.UseIISPlatformHandler(); ap

Set up the build

Set up the build Right click on the project and click New Item. Then choose TypeScript Configuration File and use the default name tsconfig.json. Replace the default tsconfig.json with the following: { "compilerOptions": { "noImplicitAny": true, "noEmitOnError": true, "sourceMap": true, "target": "es5", "outDir": "./Scripts/App" }, "files": [ "./src/app.ts", ], "compileOnSave": true } This is similar to the default, with the following differences: It sets "noImplicitAny"