TIL - Code Quality
February 18, 2020
What follows is notes from a class. Incomplete thoughts & code that probably will only make sense to me. Given enough time, even that may be a stretch! Reference at your own risk 😂
Slides
Typescript Slides
Typescript
- Instead of
tsc --watch
VS Code can run a build task:cmd
+shift
+P
then type “tasks” and select ‘Tasks: Configure Default Build Step’, ‘tsc:watch - tsconfig.json’ which this will produce a.vscode
folder with tasks.json - Terminal –> Run Build Task
Tuples
Tuples are like arrays where you specify the type of some of the items, though it can have items which don’t have types. Only the defined indexes will be type-checked.
TypeScript Types
- number
- string
- boolean
- symbol
- null
- undefined
- Function
- object
- Array<genericType>
- tuple
- enum
- void
- never
- unknown
- any (avoid this as it defeats the whole purpose of TS!)
Union Types
let str: string | number; |
Object Types - Interfaces
Interfaces allow us to define group definitions.
interface Person { |
Optional properties are identified with a question mark (?) –> you’ll only get an error on the required ones if they’re not there:
interface Animal { |
Functions
// Define an interface for your object |
Function Type Declarations
// this won't be run, it's a type declaration for the function |
Example:
type numericFn = (num: number) => number; |
External Code Types
If you want to use TypeScript with 3rd party code you can import types into your project. For example search them on Type Search…here’s an example for lodash
(npm i @types/lodash
). Definitely Typed is another resource for commonly used type definitions.
TypeScript in Bigger Projects
Run this to see an example of TS and the file structure in a bigger code project:
npx create-react-app app-with-typescript --template typescript |
VS Code Shell And Path
Not related to the above but exciting nonetheless! This turned out to tbe the fix to make the bash shell work correctly in VS Code. For whatever reason it was not putting the correct node version into my path with nvm
, but this made it work properly.
We also got eslint
working properly by deleting a duplicate installation of node. We compared which versions were being picked up by running which node
in the different shell windows, finding an old version 8 installation of node in usr/local/bin
and then deleting it. Hoping this won’t cause issues with yarn working, since crew installed this Node version when installing yarn.