Setting up a TS Project
- Create project folder
a.
mkdir ts-intro
- Initialize npm
a.
npm init -y
- Install Typescript
a.
npm i --save-dev typescript
- Create src folder and index.ts file
a.
mkdir src && touch src/index.ts
- Set up npm script
a. open package.json
b. remove test script
c. add start script
i.
"start": "tsc && node dist/index.js"
- Initialize Typescript project
a.
node_modules/typescript/bin/tsc --init
- Edit tsconfig.json
a.
"rootDir": "./src"
b."outDir": "./dist"
- Edit index.ts and add a
console.log
statement - Run
npm start
to see your message and verify there are no errors
Notes
Import and Export
Typescript uses the 'import' and 'export' keywords. Do not use the old 'require' function to bring in modules and instead use 'import'. This also means you export with the 'export' keyword.
Example:
file1.ts
import { exportedValue } from './file2';
...
file2.ts
...
export const exportedValue = 42;
Interface Name
When naming an interface: Camel case starting with a capital letter
Example an interface that is used to create a user might be named CreateUserPayload
Interface File Name
When naming an interface file: All lowercase with hypens (-) separating words followed by .interface.ts
Example The interface above would have a file name of create-user-payload.interface.ts