Ts 01

   Author: Gentleman.Hu
   Create Time: 2020-11-08 22:29:31
   Modified by: Gentleman.Hu
   Modified time: 2020-11-10 23:57:02
   Email: justfeelingme@gmail.com
   Home: https://crushing.xyz
   Description: TypeScript Start

TypeScript Start 01

https://www.youtube.com/watch?v=BwuLxPH8IDs

Basic

Init

  • npm init

  • npm install --save-dev lite-server

  • npm start

Core Types

  • ts中静态类型,可在编译期检查到类型不匹配错误

  • js动态类型,随意转换

function add(n1: number, n2: number){
  if(typeof n1 !== 'number' || typeof n2 !== 'number'){
    throw new Error('Incorrect input!');
  }
  return n1 + n2;
}

const number1 = '5'
const number2 = 2.5;

const result = add(number1,number2);
console.log(result);

如上代码,在ts中,写代码期间(编译前期间)就可被编译器检查到类型不匹配.

一些疑问与探索

  • html标签中<script src=" " defer>defer啥意思?

    defer_in_html

Definition and Usage
The defer attribute is a boolean attribute.

When present, it specifies that the script is executed when the page has finished parsing.

Note: The defer attribute is only for external scripts (should only be used if the src attribute is present).

Note: There are several ways an external script can be executed:

If async is present: The script is executed asynchronously with the rest of the page (the script will be executed while the page continues the parsing)
If async is not present and defer is present: The script is executed when the page has finished parsing
If neither async or defer is present: The script is fetched and executed immediately, before the browser continues parsing the page
There are (at least) two types of package dependencies you can indicate in your package.json files:

Those packages that are required in order to use your module are listed under the "dependencies" property. Using npm you can add those dependencies to your package.json file this way:

npm install --save packageName
Those packages required in order to help develop your module are listed under the "devDependencies" property. These packages are not necessary for others to use the module, but if they want to help develop the module, these packages will be needed. Using npm you can add those devDependencies to your package.json file this way:

npm install --save-dev packageName

Last updated