标签:war atomic anything bool 软件包 npm 查看 his 区别
本教程将需要Node.js和Npm。如果尚未安装,请参见这里。
npm install
接下来可以打开终端并运行tsc -v,查看其是否已正确安装。
tsc -v Version 1.8.10
tsc main
也可以列出所有文件,或应用通配符来一次编译多个文件:
# Will result in separate .js files: main.js worker.js. tsc main.ts worker.ts # Compiles all .ts files in the current folder. Does NOT work recursively. tsc *.ts
# Initializes a watcher process that will keep main.js up to date.
tsc main.ts --watch
var burger: string = ‘hamburger‘, // String calories: number = 300, // Numeric tasty: boolean = true; // Boolean // Alternatively, you can omit the type declaration: // var burger = ‘hamburger‘; // The function expects a string and an integer. // It doesn‘t return anything so the type of the function itself is void. function speak(food: string, energy: number): void { console.log("Our " + food + " has " + energy + " calories."); } speak(burger, calories);
// JavaScript code from the above TS example. var burger = ‘hamburger‘, calories = 300, tasty = true; function speak(food, energy) { console.log("Our " + food + " has " + energy + " calories."); } speak(burger, calories);
// The given type is boolean, the provided value is a string. var tasty: boolean = "I haven‘t tried it yet";
main.ts(1,5): error TS2322: Type ‘string‘ is not assignable to type ‘boolean‘.
function speak(food: string, energy: number): void{ console.log("Our " + food + " has " + energy + " calories."); } // Arguments don‘t match the function parameters. speak("tripple cheesburger", "a ton of");
// Here we define our Food interface, its properties, and their types. interface Food { name: string; calories: number; } // We tell our function to expect an object that fulfills the Food interface. // This way we know that the properties we need will always be available. function speak(food: Food): void{ console.log("Our " + food.name + " has " + food.calories + " calories."); } // We define an object that has all of the properties the Food interface expects. // Notice that types will be inferred automatically. var ice_cream = { name: "ice cream", calories: 200 } speak(ice_cream);
interface Food { name: string; calories: number; } function speak(food: Food): void{ console.log("Our " + food.name + " has " + food.calories + " grams."); } // We‘ve made a deliberate mistake and name is misspelled as nmae. var ice_cream = { nmae: "ice cream", calories: 200 } speak(ice_cream);
main.ts(16,7): error TS2345: Argument of type ‘{ nmae: string; calories: number; } is not assignable to parameter of type ‘Food‘. Property ‘name‘ is missing in type ‘{ nmae: string; calories: number; }‘.
class Menu { // Our properties: // By default they are public, but can also be private or protected. items: Array<string>; // The items in the menu, an array of strings. pages: number; // How many pages will the menu be, a number. // A straightforward constructor. constructor(item_list: Array<string>, total_pages: number) { // The this keyword is mandatory. this.items = item_list; this.pages = total_pages; } // Methods list(): void { console.log("Our menu for today:"); for(var i=0; i<this.items.length; i++) { console.log(this.items[i]); } } } // Create a new instance of the Menu class. var sundayMenu = new Menu(["pancakes","waffles","orange juice"], 1); // Call the list method. sundayMenu.list();
class HappyMeal extends Menu { // Properties are inherited // A new constructor has to be defined. constructor(item_list: Array<string>, total_pages: number) { // In this case we want the exact same constructor as the parent class (Menu), // To automatically copy it we can call super() - a reference to the parent‘s constructor. super(item_list, total_pages); } // Just like the properties, methods are inherited from the parent. // However, we want to override the list() function so we redefine it. list(): void{ console.log("Our special menu for children:"); for(var i=0; i<this.items.length; i++) { console.log(this.items[i]); } } } // Create a new instance of the HappyMeal class. var menu_for_children = new HappyMeal(["candy","drink","toy"], 1); // This time the log message will begin with the special introduction. menu_for_children.list();
// The <T> after the function name symbolizes that it‘s a generic function. // When we call the function, every instance of T will be replaced with the actual provided type. // Receives one argument of type T, // Returns an array of type T. function genericFunc<T>(argument: T): T[] { var arrayOfT: T[] = []; // Create empty array of type T. arrayOfT.push(argument); // Push, now arrayOfT = [argument]. return arrayOfT; } var arrayFromString = genericFunc<string>("beep"); console.log(arrayFromString[0]); // "beep" console.log(typeof arrayFromString[0]) // String var arrayFromNumber = genericFunc(42); console.log(arrayFromNumber[0]); // 42 console.log(typeof arrayFromNumber[0]) // number
var sayHi = function(): void { console.log("Hello!"); } export = sayHi;
import sayHi = require(‘./exporter‘); sayHi();
tsc --module amd *.ts
npm install -g typescript @ beta
原文参见:30分钟学会TypeScript
标签:war atomic anything bool 软件包 npm 查看 his 区别
原文地址:https://www.cnblogs.com/mareden/p/13232980.html