typescript初学

这篇博客介绍了TypeScript的基础知识,包括其作为JavaScript超集的特点,如强类型和类型安全,以及如何进行基本配置和使用。内容涵盖了数据类型、标准库、错误消息的本地化、作用域、对象类型、元组、枚举、函数、接口、类、访问修饰符、抽象类和泛型等概念。TypeScript广泛应用于Angular和Vue.js等框架。
摘要由CSDN通过智能技术生成

强类型和弱类型(类型安全)
静态类型和动态类型

class Main{
   
    static void foo(int num){
   
        System.out.println(num);
    }
    public static void main(String[] args){
   
        Main.foo(100) // ok
        Main.foo("100") // ok
        Main.foo(parseInt("100")) // oK
    }
}

强类型有更强的类型约束,而弱类型中就没有什么约束
强类型不允许有任意的类型转换

Typescript 是js的增强版,是js的超集
TS包含了ES6+及JS及类型系统
最后会编译成原始的JS,js运行环境都支持TS运行
Anguler/vue.js都支持

基本使用

// 可以完全按照js 标准语法编写代码
const hello = (name:string) =>{
   
    console.log(`hello ${
     name}`)
}
hello(100)

配置文件
tsc --init 命令生成tsconfig.js文件

{
   
  "compilerOptions": {
     // 编译器的配置选项
    /* Visit https://aka.ms/tsconfig.json to read more about this file */
    /* Basic Options */
    // "incremental": true,                   /* Enable incremental compilation */
    "target": "es5",         //设置编译后的js版本                 /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
    "module": "commonjs",    //输出的代码以何种方式模块化                 /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
    // "lib": [],                             /* Specify library files to be included in the compilation. */
    // "allowJs": true,                       /* Allow javascript files to be compiled. */
    // "checkJs": true,                       /* Report errors in .js files. */
    // "jsx": "preserve",                     /* Specify JSX code generation: 'preserve', 'react-native', or 'react'. */
    // "declaration": true,                   /* Generates corresponding '.d.ts' file. */
    // "declarationMap": true,                /* Generates a sourcemap for each corresponding '.d.ts' file. */
    "sourceMap": true,  // 开启源代码映射                   /* Generates corresponding '.map' file. */
    // "outFile": "./",                       /* Concatenate and emit output to single file. */
    "outDir": "dist",   //编译结果输出的文件夹                     /* Redirect output structure to the directory. */
    "rootDir": "src",    //配置源代码存放位置                   /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
    // "composite": true,                     /* Enable project compilation */
    // "tsBuildInfoFile": "./",               /* Specify file to store incremental compilation information */
    // "removeComments": true,                /* Do not emit comments to output. */
    // "noEmit": true,                        /* Do not emit outputs. */
    // "importHelpers": true,                 /* Import emit helpers from 'tslib'. */
    // "downlevelIteration": true,            /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
    // "isolatedModules": true,               /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
    /* Strict Type-Checking Options */
    "strict": true,    //开启严格模式                       /* Enable all strict type-checking options. */
    // "noImplicitAny": true,                 /* Raise error on expressions and declarations with an implied 'any' type. */
    // "strictNullChecks": true,              /* Enable strict null checks. */
    // "strictFunctionTypes": true,           /* Enable strict checking of function types. */
    // "strictBindCallApply": true,           /* Enable strict 'bind', 'call', and 'apply' methods on functions. */
    // "strictPropertyInitialization": true,  /* Enable strict checking of property initialization in classes. */
    // "noImplicitThis": true,                /* Raise error on 'this' expressions with an implied 'any' type. */
    // "alwaysStrict": true,                  /* Parse in strict mode and emit "use strict" for each source file. */
    /* Additional Checks */
    // "noUnusedLocals": true,                /* Report errors on unused locals. */
    // "noUnusedParameters": true,            /* Report errors on unused parameters. */
    // "noImplicitReturns": true,             /* Report error when not all code paths in 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值