03. New features of TypeScript parameters

  1. variable type
    The typeTest.ts file is as follows
    /**String type*/
    var myname: string = "xiaoming";
    
    /**Number type*/
    var age: number = 15;
    
    /**Boolean type*/
    var man: boolean = true;
    
    /**any type*/
    var alias:any = "alias";
    alias = 13;
    
    /**Method void type*/
    function test(name: string): void{
        
    }
    
    /**Custom type*/
    class Person{
        name: string;
        age: number;
    }
    
    var zhangsan: Person = new Person();
    zhangsan.age = 18;
    zhangsan.name = 'zhangsan';
     The compiled typeTest.js file is as follows
    /**String type*/
    var myname = "xiaoming";
    /**Number type*/
    var age = 15;
    /**Boolean type*/
    var man = true;
    /**any type*/
    var alias = "alias";
    alias = 13;
    /**Method void type*/
    function test(name) {
    }
    /**Custom type*/
    var Person = /** @class */ (function () {
        function Person() {
        }
        return Person;
    }());
    var zhangsan = new Person ();
    zhangsan.age = 18;
    zhangsan.name = 'zhangsan';
     
  2. parameter default value
    The content of the defaultValue.ts file is as follows
    function test(a: string,b: string, c:string="zzz" ){
        console.log(a);
        console.log(b);
        console.log(c);
    }
    
    test('aaa', 'bbb');
    test('aaa', 'bbb', 'ccc');
      编译后defaultValue.js文件内容如下
    function test(a, b, c) {
        if (c === void 0) { c = "zzz"; }
        console.log(a);
        console.log(b);
        console.log(c);
    }
    test('aaa', 'bbb');
    test('aaa', 'bbb', 'ccc');
     
  3. 可选参数
    optionalParameters.ts文件内容如下
    /**加?设置可选参数 */
    function test(a: string,b?: string, c:string="zzz" ){
        console.log(a);
        console.log(b);
        console.log(c);
    }
    
    test('aaa', 'bbb');
    test('aaa');
    
      编译后optionalParameters.js文件内容如下
    function test(a, b, c) {
        if (c === void 0) { c = "zzz"; }
        console.log(a);
        console.log(b);
        console.log(c);
    }
    test('aaa', 'bbb');
    test('aaa');
     

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326171394&siteId=291194637