Tsis siv neeg compile nyob TypeScript hauv kev Studio Code tuav tseg

Nyob rau hauv tsev xa no peb yuav leb ob: 1) hais kom ua “TSC” tsis pom nyob rau hauv cov VS integrated davhlau ya nyob twg txoj, thiab 2) compiling tsis ua hauj lwm yeej yog txuag cia “.TS” (Tsab ntawv hom).

Taw qhia

Cia li mus contextualize, tus TypeScript, tshiab uas tso cai rau koj sau txoj siv tus qauv typed xav thiab yuav tsum muaj txoj compiled rau JavaScript ntshiab. Twb tus Nrig txog kev pom Studio Code yog ib tus ntoo khaub lig-platform chaws editor tsim los ntawm Microsoft.

// Versions siv no ncej:
- Nrig txog kev pom Studio (Vs) Chaws, xyoo tshiab: Version 1.14.1
- Typescript, tshiab uas: Version 2.4.1

Yog ib ntsis yooj yim, tab sis, koj yuav muaj teeb meem yog hais tias koj tsis paub koj nyob nraum ua dab: kuv qhov teeb meem :(D)

Plahaum Node.js thiab TypeScript

Ib yam rau nruab TypeScript uas yog los ntawm tus neeg saib xyuas pob Node.js (Npm – Ntawm nws tus nai), tab sis yuav siv txoj kev no, koj xub xav nruab ib lub Node.js.

Tom qab kev txhim kho ntawm, qhib cov davhlau ya nyob twg (hais kom ua) thiab khiav rau nram qab no hais kom ua 􀂃 cia li nruab TypeScript:

nruab npm -typescript g

Tseem ntawm lub qhov rais davhlau ya nyob twg (los yog Linux), ua ib TypeScript me ntsuam xyuas los ntawm khij tus installed version. Qhov hais kom ua “TSC” yog ib tug acronym rau TypeScript Compiler.

TSC -v

Teeb meem

Qhov teeb meem ntawd yog cov hais kom ua “TSC” ua haujlwm feem ntau ntawm cov qhov rai davhlau ya nyob twg, tab sis tsis nyob ntawm integrated davhlau ya nyob twg uas tawm tsis pub dhau lub Studio Code siv qhov muag saib nws tus kheej (Chaws VS), displaying qhov yuam kev no:

'tsc' yog tsis paub tus kev hais kom ua sab hauv los sis lwm,qhov kev pab cuam operable los yog batch cov ntaub ntawv.

tsis tas li ntawd, Kuv tsis xav kom khiav qhov hais kom ua “TSC” rau txhua lub sij hawm koj xav compile tej ntaub ntawv ib “.TS” (Extension kom TypeScript). Li ntawd, li cas los yeej ua tsis zoo no feem muab tso ua ke thaum txuag (CTRL + S)? Ntawd yog dab tsi peb mam li saib tom ntej.

Lwm tus uas tsis

Sim yuav kom daws tau tej teeb meem no, pom Posts paub siv lub compileOnSave, tab sis qhov no yog tsuas txaus siab los siv qhov muag saib lub Studio 2015 nrog TypeScript > = 1.8.4 thiab atom-typescript plugin. IE, tsis siv compileOnSave Nyob rau hauv “tsconfig.json” nrig txog kev pom studio chaws, cov cai hauv qab no yuav tsis ua haujlwm:

{
   "compileOnSave": tseeb,
   "compilerOptions": {
       "noImplicitAny" : tseeb
   }
}

Tov

TSC hauv lub VS Code Integrated Terminal

Thawj zaug wb daws tau cov teeb meem ntawm TSC tsis tau pom tau hais tias nyob rau Integrated kev Studio Code davhlau ya nyob twg (Chaws VS).

Ib co Posts muab txoj kev tsc los yog npm kab chaw kuj sib txawv thiab hais, tiam sis ib txoj kev yooj yim rau kev daws kom tau qhov no yog los ntawm kev ua cov cov plahaum ntawm tus “TypeScript rau Studio nrig txog kev pom, Nws yog ib qho tau tshaj rau VS Code thiab.

Tam sim no muaj, tom qab tus plahaum, qhib rau integrated kev Studio Code davhlau ya nyob twg, Typing:

// Hotkey rau siv qhov muag saib lub studio chaws integrated davhlau ya nyob twg:
Ctrl+`
// Los yog siv cov zaub mov qhib lub davhlau ya nyob twg hauv kev Studio Code:
Pom, pom tshiab > Integrated Terminal

Mus xyuas cov TypeScript version ncaj nraim rau tus integrated VS Code davhlau ya nyob twg (Tsis nyob rau hauv lub qhov rais davhlau ya nyob twg), thiab tej zaum yuav raug ua hauj lwm:

TSC -v

Tsis siv neeg muab tso ua ke thaum txuag tau ib daim ntawv .TS

Los ntawm cov VS Code davhlau ya nyob twg, position koj tus kheej hauv koj tsab ntawv directory (.TS), Piv txwv:

C cd:\wamp64wwwts

Sau ntawv tsconfig.json typing hauv cov VS Code davhlau ya nyob twg:

TSC --init

Nco ntsoov tias ib cov ntaub ntawv “tsconfig.json” yuav yog tsim nyob rau hauv “(C):\wamp64wwwts” (tam sim no directory).

Qhib lub tsconfig.json zoo thiab pab tau cov kev xaiv “sourceMap”: tseeb, es tseg cov kev hloov kho, tsis zoo li no:

  "compilerOptions": {
    /* Kev xaiv yooj yim */
    "target": "es5",                          /* Qhia kom meej ECMAScript phiaj version: 'ES3' (vim), 'ES5', 'ES2015', 'ES2016', 'ES2017', los 'ESNEXT'. */
    "module": "commonjs",                     /* Qhia kom meej module chaws tiam: 'commonjs', 'amd', 'qhov system', 'umd', 'es2015', los 'ESNext'. */
    // "lib": [],                             /* Thov qhia kev qiv ntaub ntawv xam nrog rau qhov muab tso ua ke:  */
    // "allowJs": tseeb,                       /* Pub javascript ntaub ntawv yuav tsum compiled. */
    // "checkJs": tseeb,                       /* Daim ntawv qhia txog uas tsis nyob hauv ntaub ntawv .js. */
    // "jsx": "preserve",                     /* Qhia kom meej JSX chaws tiam: 'khaws cia', 'hnov mob-neeg', los 'hnov mob'. */
    // "declaration": tseeb,                   /* Generates coj '. ntaub ntawv d.ts txiv. */
    "sourceMap": tseeb,                     /* Generates coj daim '.map'. */
    // "outFile": "./",                       /* Concatenate thiab emit tso zis rau ib leeg. */
    // "outDir": "./",                        /* Redirect tso zis ua qauv rau cov directory. */
    // "rootDir": "./",                       /* Thov qhia cov hauv paus hniav me nyuam rau npe input ntaub ntawv. Siv kom tswj tau cov zis directory qauv uas--outDir. */
    ... continua ...

Npaj txhij, agora é só ir tsis davhlau ya nyob twg puas VS Code e digitar o comando:

TSC -w

Este comando fica monitorando (ou vendo, por isso “w” saib tsib) ib aplicação e qualquer alteração tsis muaj arquivos .TS, quando salvo, yuav ua rau cov chaw khiav dejnum tswj muaj nyob rau hauv .Js.

Tus ntawd yog nws!

Tag nrho hits: 10691

sau ntawv cia

Qhov koj email chaw nyob yuav tsis muab luam tawm. Search yog cim muaj *