TypeScript

  • R7_956533
    了解作者
  • 3.1KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-11 12:08
    上传日期
打字稿 打开命令提示符,然后键入:sudo npm install -g typescript(要在您的计算机中全局安装TypeScript) 编译TypeScript文件:tsc app.ts 类型运算符 在TypeScript中,您一直都在使用字符串或数字之类的类型。 重要说明:它是字符串和数字(​​等),不是字符串,数字等。(TypeScript中的核心基本类型都是小写!) 核心类型 NUMBER:1、5.3,-10(所有数字,整数或浮点数之间没有区别) STRING:“嗨”,“嗨”, Hi (所有文本值) BOOLEAN:正确,错误(仅这两个,没有真实值或脆弱值) 对象:{age:30} 阵列:[1、2、3] TUPLE:[1,2](固定长度数组和固定类型) 枚举:枚举{NEW,OLD}(自动枚举的全局常量标识符) 任何; (尽可能避免,它剥夺了打字稿带给我们的
TypeScript-main.zip
  • TypeScript-main
  • practice.tsx
    1.2KB
  • README.md
    4.6KB
内容介绍
# TypeScript * Open Command Prompt and type: sudo npm install -g typescript (To install TypeScript globally in your machine) * Compile the TypeScript file: tsc app.ts * typeof operator * In TypeScript, you work with types like string or number all the times. Important: It is string and number (etc.), NOT String, Number etc. (The core primitive types in TypeScript are all lowercase!) Core Types 1. NUMBER: 1, 5.3, -10 (All numbers, no differentiation between integers or floats) 2. STRING: 'Hi', "Hi", `Hi` (All text values) 3. BOOLEAN: true, false (just these two, no truthy or flasy values) 4. OBJECT: {age:30} 5. ARRAY: [1, 2, 3] 6. TUPLE: [1,2] (fixed length array, and fixed type) 7. ENUM: enum { NEW, OLD } (automatically enumerated global constant identifiers) 8. ANY; ( avoid as much as possible, it takes away all the advantages that typescript gives us) 9. UNION TYPES: input1: number | string | boolean 10. LITERAL TYPES : resultConversion: 'as-number' | 'as-string' 11. ALIASES : type Combinable = number | string; or type ConversionDescriptor = 'as-number' | 'as-string'; type User = { name: string; age: number }; 12. function RETURN TYPES & VOID : function printResult(num: number): void { console.log('Result:'+num); } (you can define function return type explicitly or typescript infers the same) ```Undefined is a type in TypeScript``` 13. functions as types : (WE SHOULD MATCH THE TYPES) ```let combineValues: (a: number, b: number) => number;``` ```combineValues = add;``` ```function add(n1: number, n2: number){ return n1 + n2; }``` ```console.log(combineValues(8,8));``` 14. CALLBACKS : ```function addAndHandle(n1: number, n2:number, cb:(num: number)=>void) { const result=n1+n2; cb(result); }``` ```addAndHandle(10,20,(result)=>{ console.log(result); });``` 15. UNKNOWN : "unknown" is a better choice than "any". ```let userInput = unknown; let username = string;``` ```userInput = 'Max'; if(typeOf userInput === 'string') { username = userInput; }``` 16. NEVER : Another type that functions can return. ``` function generateError(message: string, code: number): never { throw {message: message, errorCode: code} }``` ```generateError('An error occurred!', 500);``` this function esssentially never produces a value. (You can be very clear and explicitly specify never as the return type to make it really clear that this never returns anything. Code quality purposes) Another function that never returns anything is function with the infinite loop: ``` while(true) {} ``` * eg: function add(n1: number, n2: number, showResult: boolean, phrase: string){ } * let number1 = 5; (We dont have explicit type assignments here, because TypeScript has a built in feature called type inference. This means TypeScript does its best to understand which type you have in a certain variable or constant) * let number1: number = 5 (Assigning the type here is not a good idea, not a good practice) * let number 1: number; (Good practice) * OBJECT : BETTER SYNTAX for object: const person = {name: 'abc', age: 100}, we can also use this declaration instead => const person: object; person = {name: 'abc', age: 100}; * ARRAY : if you want to use a mixed array - with type strings, numbers etc., declare it as => let favoriteActivities = any[]; * TUPLE : const person : { role: [number, string]; (This marks a tuple type here) } = { role: [2, 'author'] }; // Tuple tells javascript that i want a special array with exactly the number of elements and types that I specify. * ENUM : enum Role { ADMIN, READ_ONLY, AUTHOR }; ( ACCESSING => Role.ADMIN ) (OR) enum Role { ADMIN='ADMIN', READ_ONLY=100, AUTHOR='AUTHOR' }; * UNIONS : if we have some place in our application, be that a parameter of a function or a constant or a variable, where we accept two different kinds of values. Well then a union type can help us. to tell typescript we are fine with either a number or a string, we do use a "|" => ``` function combine (input1 : number | string, input2: number | string) { let result; if(typeof input1 === 'number' && typeof input2 === 'number') result = input1 + input2; else result = input1.toString() + input2.toString(); return result; } ``` * LITERAL TYPE : function combine (input1: number | string, input2: number | string, resultConversion: 'as-number' | 'as-string' ) (we want a string for result conversion, but it has to be one of these two values, any other string value will not be allowed) * ALIASES : "type" keyword is used. eg: type Combinable = number | string; => function Combine(input1: Combinable, input2: Combinable){} (Saves extra code). type User = { name: string; age: number }; => function isOlder(user: User, checkAge: number) { return checkAge > user.age; }
评论
    相关推荐