Thursday, May 30, 2019

使用簡單的tsc 編譯器來玩typescript

when you would like to learn typescript by self. you could learn it through VS 201x community, then create it with Angular. later, this utility creates several folders and includes multiple typescripts file. it is not a good idea to develop and test.

here, there is a quick guide and just focus on typescript only.
- npm install -g typescript.
check which one version install? and where it is.
command line => where tsc. 
in spite of finding a tsc installed already. but, the version is too old to compiling success.
you might get some fails.

check version & installation folder

D:\test\testTs>where tsc
C:\Users\Terence_Liu\AppData\Roaming\npm\tsc
C:\Users\Terence_Liu\AppData\Roaming\npm\tsc.cmd
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\tsc.exe
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\tsc.js


D:\test\testTs>tsc -v
Version 3.5.1

if version is 1.x.x of tsc, it means system is using default installation by VS
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\tsc.exe
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\tsc.js


ES5 and ES6 
When you would like to dedicated format, you can add extra parameter --target with it
under command line
tsc --target es5 prime.ts

let us change it
first:
Second, (below is from "System variables")





Now, let us start working a sample 
use following command to build and try following sample
1. tsc --target es5 prime.ts
2. node prime.js

Note: DO NOT use "es6" to instead of "es5"

in prime.ts
import * as NaW from './modules/NameAndWeather'

import { TempConvert } from './modules/tempConvert';
let myData3 = new NaW.Name("Terence", "Liu");
console.log(myData3.message);

console.log( TempConvert.convertFtoC(38));


in NameAndWeather.ts
export class Name {
constructor( first, second )
{
this.first = first;
this.second = second;
}
get message()
{
return `Hello ${this.first} ${this.second}`
}
first: string;
second:string;
}

export class Weather
{
constructor(weather, city)
{
this.weather = weather;
this.city = city;
}
get weatherMessage()
{
return `It is ${this.weather} in ${this.city}`;
}
city: string;
weather: string;
}


in tempConvert.ts
export class TempConvert
{
constructor()
{

}
static convertFtoC(temp : number)
{
return((parseFloat(temp.toPrecision(2))-32)/1.8).toFixed(1);
}
}

No comments:

Post a Comment