Last active
September 2, 2022 00:13
-
-
Save techthoughts2/8d574c25a8fb39d69d338095a0345b06 to your computer and use it in GitHub Desktop.
tbd - notes taken while learning Typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
- Static typing | |
- Code completion | |
- Refactoring | |
- Shorthand notations | |
transpilation | |
typescript -> javascript | |
*/ | |
// creating a configuration file for the compiler | |
// tsc --init | |
//#region variables | |
let age: number = 20 | |
console.log(age) | |
//#region types | |
// javascript: | |
// number, string, boolean, null, undefined, object | |
// typescript: | |
// any, unknown, never, enum, tuple | |
let sales: number = 123_456_789 //can use underscores for large numbers | |
let course: string = 'Typescript' | |
let is_published: boolean = true | |
let level //any - avoid. not best practice | |
// arrays | |
let numbers: number[] = [1, 2, 3] | |
// tuple | |
let user: [number, string] = [1, 'Jake'] | |
// enums - list of related constants | |
const small = 1 | |
const medium = 2 | |
const large = 3 | |
// values here are auto assigned: | |
enum Size { Small, Medium, Large } | |
// more optimized javascript code: | |
const enum Size2 { Small, Medium, Large } | |
// can explicity set values | |
enum shirtSize { Small = 2, Medium = 5, Large = 10 } | |
let mySize: Size = Size.Medium | |
console.log(mySize) | |
// objects | |
let employee: { | |
readonly id: number | |
name: string | |
retire: (date: Date) => void | |
} = { | |
id: 1, | |
name: 'Jake', | |
retire: (date: Date) => { | |
console.log(date) | |
} | |
} | |
employee.name = 'Joe' | |
//#endregion | |
//#endregion | |
// annotate what the function is going to return | |
function calculateTax(income: number): number { | |
if (income < 50_000) | |
return income * 1.2 | |
// else would be undefined here | |
// return 0 | |
} | |
// optional parameters | |
function calculateTaxS(income: number, taxYear = 2022): number { | |
if (taxYear < 2022) { | |
return income * 1.2 | |
} | |
return income * 1.3 | |
} | |
// type alias | |
type Employee = { | |
readonly id: number | |
name: string | |
retire: (date: Date) => void | |
} | |
let aEmployee: Employee = { | |
id: 1, | |
name: 'Jake', | |
retire: (date: Date) => { | |
console.log(date) | |
} | |
} | |
// union | |
function kgToLbs(weight: number | string): number { | |
// Narrowing | |
if (typeof weight === 'number') { | |
return weight * 2.2 | |
} | |
else { | |
return parseInt(weight) * 2.2 | |
} | |
} | |
kgToLbs(10) | |
kgToLbs('10kg') | |
// intersection | |
let weight: number & string | |
type Draggable = { | |
drag: () => void | |
} | |
type Resizable = { | |
resize: () => void | |
} | |
type UIWidget = Draggable & Resizable | |
let textBox: UIWidget = { | |
drag: () => { }, | |
resize: () => { } | |
} | |
// literal types - Literal (exact, specific) | |
let quantity: 50 | 100 = 100 | |
type Quantity = 50 | 100 | |
let zeQuantity: Quantity = 100 | |
type Metric = 'cm' | 'inch' | |
// nullable types | |
function greet(name: string | null) { | |
if (name) { | |
console.log(name.toUpperCase()) | |
} | |
else { | |
console.log('Hola!') | |
} | |
} | |
greet(null) | |
// null checks | |
type Customer = { | |
birthday: Date | |
} | |
function getCustomer(id: number): Customer | null { | |
return id === 0 ? null : { birthday: new Date() } | |
} | |
let customer = getCustomer(0) | |
// optional property access operator | |
console.log(customer?.birthday) | |
// optional element access operator | |
// customers?.[0] | |
// optional call | |
let log: any = null | |
log?.('a') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment