TS-Node 體驗


【給鏈接不贅述】【提醒坑】【想更簡單學計算機必須會看懂英語】【win讓你專注代碼未來深入linux】【盡管文件恨多,但是我們不去dissect 是永遠不會的】

https://www.tslang.cn/docs/handbook/typescript-in-5-minutes.html

https://github.com/Microsoft/TypeScript-Node-Starter#typescript-node-starter

https://docs.mongodb.com/manual/tutorial/install-mongodb-on-windows/

 

【錦囊妙計】npm install 如果遇到問題,請使用npm doctor 

……………………………………………………………………

   啟動 mongod

"C:\Program Files\MongoDB\Server\3.6\bin\mongod.exe" --dbpath "C:\Dev\mongodb\data"

……………………………………………………………………………………………………

npm run build == VSCODE快捷鍵ctrl+shift+b 
npm start == VSCODE快捷鍵 ctrl+shift+p 然后輸入run task選擇npm start
打開 http://localhost:3000

…………………………………………………………………………

=>跳過【Deploy 部署環節】

…………………………………………………………………………………………………………………………………………………………………………………………………………

項目結構,由於TS比較工程化,最主要的是【ts文件在src下,編譯后在dist下】

Name Description
.vscode 內建
dist Contains the distributable (or output) from your TypeScript build. This is the code you ship
node_modules Contains all your npm dependencies
src Contains your source code that will be compiled to the dist dir
src/config Passport authentication strategies and login middleware. Add other complex config code here
src/controllers Controllers define functions that respond to various http requests
src/models Models define Mongoose schemas that will be used in storing and retrieving data from MongoDB
src/public Static assets that will be used client side
src/types Holds .d.ts files not found on DefinitelyTyped. Covered more in this section
src/server.ts 程序入口
test Contains your tests. Seperate from source because there is a different build process.
views Views define how your app renders on the client. In this case we're using pug
.env.example API keys, tokens, passwords, database URI. Clone this, but don't check it in to public repos.
.travis.yml Used to configure Travis CI build
.copyStaticAssets.ts Build script that copies images, fonts, and JS libs to the dist folder
jest.config.js Used to configure Jest
package.json File that contains npm dependencies as well as build scripts
tsconfig.json Config settings for compiling server code written in TypeScript
tsconfig.tests.json Config settings for compiling tests written in TypeScript
tslint.json Config settings for TSLint code style checking

 

 …………………………………………………………………………………………………………………………………………………………………………

編譯是可以配置的。tsconfig.json

    "compilerOptions": { "module": "commonjs", "esModuleInterop": true, //導入語法 import foo from "foo" "target": "es6", "noImplicitAny": true,   //【最佳實踐】true打開的話對應我們要用任何的Library都需要.d.ts即使是空定義,放心可以下載。 "moduleResolution": "node", "sourceMap": true,    //debug用 "outDir": "dist", "baseUrl": ".", "paths": {      //默認會去掃描node_modules下@types(.d.ts文件),我們私有定義.d.ts不是下載來的要配置一下。 "*": [ "node_modules/*", "src/types/*" ] } },
"include": [  //將需要編譯的文件包含進來,也可以exclude掉 "src/**/*" ]

 …………………………………………………………………………………………………………………………………………………………………………

構建,其中package.json有可供我們調用的命令,npm run <script-name>

Npm Script Description
start Does the same as 'npm run serve'. Can be invoked with npm start
build Full build. Runs ALL build tasks (build-sassbuild-tstslintcopy-static-assets)
serve Runs node on dist/server.js which is the apps entry point
watch-node Runs node with nodemon so the process restarts if it crashes. Used in the main watch task
watch Runs all watch tasks (TypeScript, Sass, Node). Use this if you're not touching static assets.
test Runs tests using Jest test runner
watch-test Runs tests in watch mode
build-ts Compiles all source .ts files to .js files in the dist folder
watch-ts Same as build-ts but continuously watches .ts files and re-compiles when needed
build-sass Compiles all .scss files to .css files
watch-sass Same as build-sass but continuously watches .scss files and re-compiles when needed
tslint Runs TSLint on project files
copy-static-assets Calls script that copies JS libs, fonts, and images to dist directory
debug Performs a full build and then serves the app in watch mode
serve-debug Runs the app with the --inspect flag
watch-debug The same as watch but includes the --inspect flag so you can attach a debugger

 …………………………………………………………………………………………………………………………………………………………………………

.d.ts 文件用來定義類型,這是因為不是所有js都有ts版本,通過這項定義,ts將幫助編輯器獲得類型提示。

有些.d.ts甚至不需要我們操心,可以去下載 => https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types

//.d.ts日常管理

1.當npm install包的時候,對應我們去下載.d.ts文件。

【下載DefinitelyTyped】npm install --save-dev @types/jquery    (務必加選項或者-D,因為只是編譯期需要的依賴。)

2.下載不到,我們就要去tsconfig.json配置下path包含我們自己定義的.d.ts文件。

【自己定義.d.ts】參見 => https://github.com/Microsoft/dts-gen

如果沒成功生成的話,需要【跳過.d.ts類型檢查】直接在文件中寫 declare module "<some-library>";//相當於空文件

然后可以繼續一步一步改善(不能生成只好這樣) http://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html

 …………………………………………………………………………………………………………………………………………………………………………

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM