[js]Typescript

typescript

https://www.typescriptlang.org/docs/handbook/typescript-in-5-minutes.html

 

setup

setup
npm install -g typescript

file (.ts)
xxx.ts

compile to .js
tsc  xxx.ts

 

sample
 
greeter.js
function greeter(person) {
return"Hello, "+person;
}
var user = "Jane User";
//# warning
//let user = [ "Jane User","b","c"];
document.body.innerHTML = greeter(user);

interface/class

class Student {
    fullName: string;
    constructor(public firstName: string, public middleInitial: string, public lastName: string) {
        this.fullName = firstName + " " + middleInitial + " " + lastName;
    }
}

interface Person {
    firstName: string;
    lastName: string;
}

function greeter(person: Person) {
    return "Hello, " + person.firstName + " " + person.lastName;
}

// #interface
//let user = { firstName: "Jane", lastName: "User" };

// #class
let user = new Student("Jane", "M.", "User");

document.body.innerHTML = greeter(user);

 

Leave a Reply