One. What isTypeScript
TypeScript is a superset of Javascript that follows the latest ECMAScript specification. Typescript extends Javascript syntax, not replaces it.
Typescript
To enhance theJavaScript
Grammar,JS
More rigorous, letJS
More suitable for large applications- TS provided
The type system
, can help us provide accurate at development timeCode hinting
- Code is done during development
Type checking
To avoid many potential errors TS
Is not run directly, you need to compile to run
2. Environment Configuration
2.1 buildts
file
Globally install typescript ts files for compilation
npm install typescript -g
tsc --init # generate tsconfig. Json
tsc You can compile ts files into JS files
tsc --watch # Monitor ts file changes to generate JS files
Copy the code
2.2 configurationrollup
The environment
Install dependencies
npm install rollup typescript rollup-plugin-typescript2 @rollup/plugin-node-resolve rollup-plugin-serve -D
Copy the code
Example Initialize the TS configuration file
npx tsc --init
Copy the code
A rollup configuration
// rollup.config.js
import ts from 'rollup-plugin-typescript2'
import {nodeResolve} from '@rollup/plugin-node-resolve';
import serve from 'rollup-plugin-serve';
import path from 'path'
export default {
input:'src/index.ts'.output: {format:'iife'.file:path.resolve('dist/bundle.js'),
sourcemap:true
},
plugins:[
nodeResolve({
extensions: ['.js'.'.ts']
}),
ts({
tsconfig:path.resolve(__dirname,'tsconfig.json')
}),
serve({
open:true.openPage:'/public/index.html'.port:3000.contentBase:' '}})]Copy the code
Package. The json configuration
"scripts": {
"dev": "rollup -c -w"
}
Copy the code
This will start the service for compilation with NPM run dev
2.3 Method of Resolving TS
Ts plug-in
babel
parsingrollup
It is usually usedrollup-plugin-typescript2
webpack
General usets-loader
/babel-plugin-typescript