Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
b84ca72
Update TypeScript configuration to target ES2022 and use NodeNext mod…
incognito0007 Apr 8, 2026
d91d252
Add rootDir option to TypeScript configuration
incognito0007 Apr 8, 2026
8d67b7c
Refactor variable declarations and type annotations for clarity and c…
incognito0007 Apr 8, 2026
5f2066d
Understanding Object, Arrays and tuples
incognito0007 Apr 9, 2026
7326d1f
Add tuple example for student information and destructuring assignment
incognito0007 Apr 9, 2026
c7fe12c
Add example of array usage for student information and array destruct…
incognito0007 Apr 9, 2026
81a054d
Add example usage of Truck class and update printCar function calls
incognito0007 Apr 9, 2026
0690817
Refactor Car and Truck class constructors for improved instantiation …
incognito0007 Apr 15, 2026
a0fd8e8
- Understanding Union and Intersection in typescript
incognito0007 Apr 15, 2026
3194e53
- understanding interfaces and type aliases
incognito0007 Apr 15, 2026
aa7fe1d
Refactor JSON type definitions for improved clarity and correctness
incognito0007 Apr 15, 2026
5479988
Refactor type definitions and improve code clarity in 09-type-queries.ts
incognito0007 Apr 15, 2026
9aab8a3
Refactor code structure and improve formatting in 10-callables-and-co…
incognito0007 Apr 15, 2026
b7aad71
- understanding class, access modifiers, cunstructor, overrride in ty…
incognito0007 Apr 17, 2026
8685543
- Understanding typegaurd in typescript
incognito0007 Apr 17, 2026
ff0338c
- Understanding generics in typescript
incognito0007 Apr 17, 2026
2a92870
Remove unused dictionary utilities and sample data from 14-dict-map-f…
incognito0007 Apr 17, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 36 additions & 46 deletions packages/notes-ts-fundamentals-v4/src/03-variables-and-values.ts
Original file line number Diff line number Diff line change
@@ -1,69 +1,59 @@
//* Variable Declarations & Inference
let temperature = 6 //! inference
/*
// temperature = "warm" //! type-checking

temperature = 'warm' //! type-checking
// const humidity = 79 //! literal type

//* A type as a set of allowed values
/*
// temperature = 23 //✔️ (1) - re-assignability of a let
// temperature = humidity; //! (2) - type-checking
// humidity = temperature; //! (3) - number is not of type `79`
// humidity = 79; //✔️ (4) - 79 is of type `79`
// humidity = 78; //! (5) - 78 is not of type `79`


/*
// let temp2 = 19; //! temp2's type is { all numbers }
// let humid2 = 79 as const; //! humidity's type is { 79 }
// temp2 = 23; //! Is each member in { 23 } also in { all numbers }?
// temp2 = humid2; //! Is each member in { 79 } also in { all numbers }?
// humid2 = temp2; //! Is each member in { all numbers } also in { 79 }?
// humid2 = 79; //! Is each member in { 79 } also in { 79 }
// humid2 = 78; //! Is each member in { 78 } also in { 79 }

temperature = 23 //✔️ (1) - re-assignability of a let
temperature = humidity //! (2) - type-checking
humidity = temperature //! (3) - number is not of type `79`
humidity = 79 //✔️ (4) - 79 is of type `79`
humidity = 78 //! (5) - 78 is not of type `79`

let temp2 = 19 //! temp2's type is { all numbers }
let humid2 = 79 as const //! humidity's type is { 79 }
temp2 = 23 //! Is each member in { 23 } also in { all numbers }?
temp2 = humid2 //! Is each member in { 79 } also in { all numbers }?
humid2 = temp2 //! Is each member in { all numbers } also in { 79 }?
humid2 = 79 //! Is each member in { 79 } also in { 79 }
humid2 = 78 //! Is each member in { 78 } also in { 79 }

//* Implicit `any` and type annotations
/*
// between 500 and 1000
// export const RANDOM_WAIT_TIME =
// Math.round(Math.random() * 500) + 500

// let startTime = new Date()
// let endTime
// between 500 and 1000
export const RANDOM_WAIT_TIME = Math.round(Math.random() * 500) + 500

// setTimeout(() => {
// endTime = 0
// endTime = new Date()
// }, RANDOM_WAIT_TIME)
let startTime = new Date()
let endTime: Date

setTimeout(() => {
// endTime = 0 //! Type 'number' is not assignable to type 'Date'
endTime = new Date()
}, RANDOM_WAIT_TIME)

//* Type Casting
/*
// let frontEndMastersFounding = new Date("Jan 1, 2012")
// let date1 = frontEndMastersFounding
// let date2 = frontEndMastersFounding as any;

/*
// const humid3 = 79 as number; //✔️ is 79 a number? If so, this is safe!

let frontEndMastersFounding = new Date('Jan 1, 2012')
let date1 = frontEndMastersFounding
let date2 = frontEndMastersFounding as any

/*
// let date3 = "oops" as any as Date //! TypeScript thinks this is a Date now, but it's really a string
// date3.toISOString() //! what do we think will happen when we run this? 💥
const humid3 = 79 as number //✔️ is 79 a number? If so, this is safe!

/*
// let date4 = "oops" as Date
let date3 = 'oops' as unknown as Date //! TypeScript thinks this is a Date now, but it's really a string
date3.toISOString() //! what do we think will happen when we run this? 💥

let date4 = 'oops' as Date //! TypeScript thinks this is a Date now, but it's really a string

//! Function arguments and return values

function add(a: number, b: number): number {
return a + b // strings? numbers? a mix?
}

// function add(a, b) {
// return a + b // strings? numbers? a mix?
// }

// const result = add(3, "4")
// const p = new Promise(result);
const result = add(3, 4)
const p = new Promise(result)

/**/

Expand Down
208 changes: 116 additions & 92 deletions packages/notes-ts-fundamentals-v4/src/04-objects-arrays-and-tuples.ts
Original file line number Diff line number Diff line change
@@ -1,130 +1,154 @@
//* Objects

const myCar = {
make: 'Toyota',
model: 'Corolla',
year: 2002,
}

let car: {
make: string
model: string
year: number
}
} = myCar

/*
//? A function that prints info about a car to stdout
// function printCar(car: {
// make: string
// model: string
// year: number
// }) {
// console.log(`${car.make} ${car.model} (${car.year})`)
// }
// ? A function that prints info about a car to stdout
function printCar(car: {
make: string
model: string
year: number
chargeVoltage?: number
}) {
let str = `${car.make} ${car.model} (${car.year})`
if (typeof car.chargeVoltage !== 'undefined')
str += `// ${car.chargeVoltage}v`
console.log(str)
}

// printCar(car)
printCar(car)

/*
//* Optional properties
//? Insert into function printCar
// let str = `${car.make} ${car.model} (${car.year})`
// car.chargeVoltage
// if (typeof car.chargeVoltage !== "undefined")
// str += `// ${car.chargeVoltage}v`

/*
// printCar({ //? original fn works
// make: "Honda",
// model: "Accord",
// year: 2017,
// })

// printCar({ //? optional property works too!
// make: "Tesla",
// model: "Model 3",
// year: 2020,
// chargeVoltage: 220,
// })

/*

printCar({
//? original fn works
make: 'Honda',
model: 'Accord',
year: 2017,
})

printCar({
//? optional property works too!
make: 'Tesla',
model: 'Model 3',
year: 2020,
chargeVoltage: 220,
})

//* Excess property checking

// printCar({
// make: "Tesla",
// model: "Model 3",
// year: 2020,
// color: "RED", //? EXTRA PROPERTY
// })
printCar({
...{
//? original fn works if we use spread syntax
make: 'Tesla',
model: 'Model 3',
year: 2020,
color: 'RED', //? EXTRA PROPERTY
},
})

// above code can also be written as:
printCar({
...{
make: 'Tesla',
model: 'Model 3',
year: 2020,
},
...{ color: 'RED' },
})

/*
//* Index signatures

//? Dictionary of phone #s
// const phones = {
// home: { country: "+1", area: "211", number: "652-4515" },
// work: { country: "+1", area: "670", number: "752-5856" },
// fax: { country: "+1", area: "322", number: "525-4357" },
// }
/*
//? Model as an index signature
// const phones: {
// [k: string]: {
// country: string
// area: string
// number: string
// }
// } = {}
const phones: {
mobile: {
country: string
area: string
number: string
}
[k: string]: {
country: string
area: string
number: string
}
} = {
home: { country: '+1', area: '211', number: '652-4515' },
work: { country: '+1', area: '670', number: '752-5856' },
mobile: { country: '+1', area: '322', number: '525-4357' },
}

console.log(phones.home.country)
console.log(phones.work)
console.log(phones.fax) //! undefined, but no error!
console.log(phones.mobile) //! still works, even though we have an index signature!

//* Array Types

/*
// const fileExtensions = ["js", "ts"]
const fileExtensions = ['js', 'ts']
// ^? string[]

// const cars = [ //? Let's look at an array of objects
// {
// make: "Toyota",
// model: "Corolla",
// year: 2002,
// },
// ]

const cars = [
//? Let's look at an array of objects
{
make: 'Toyota',
model: 'Corolla',
year: 2002,
},
]

//* Tuples
/*
// let myCar = [
// 2002, // Year
// "Toyota", // Make
// "Corolla" // Model
// ]
// const [year, make, model] = myCar //✔️ Destructuring

let myCar2 = [
2002, // Year
'Toyota', // Make
'Corolla', // Model
]
const [year, make, model] = myCar2 //✔️ Destructuring

//? Inference doesn't work very well for tuples
/*
// myCar = ["Honda", 2017, "Accord", "Sedan"] //! Wrong convention
/*
// let myCar: [number, string, string] = [
// 2002,
// "Toyota",
// "Corolla",
// ]
// myCar = ["Honda", 2017, "Accord"] //! Wrong convention
// myCar = [2017, "Honda", "Accord", "Sedan"] //! Too many elements

myCar2 = ['Honda', 2017, 'Accord', 'Sedan'] //! Wrong convention

let myCar3: [number, string, string] = [2002, 'Toyota', 'Corolla']
myCar3 = ['Honda', 2017, 'Accord'] //! Wrong convention
myCar3 = [2017, 'Honda', 'Accord', 'Sedan'] //! Too many elements

let studInfo: [string, number, boolean] = ['Alice', 12345, true]
const [name, id, isEnrolled] = studInfo

let studArr = ['Alice', 'Ankit', 'Anand'] //? Inference: string[]
const [stud1, stud2, stud3, stud4] = studArr //! stud4 is undefined, but no error!

//* `readonly` tuples
/*
// const numPair: [number, number] = [4, 5]; //✔️ Valid
// const numTriplet: [number, number, number] = [7]; //! Invalid

// [101, 102, 103].length //? number[].length
// numPair.length //? [number, number] length
const numPair: [number, number] = [4, 5] //✔️ Valid
const numTriplet: [number, number, number] = [7][(101, 102, 103)] //! Invalid
.length //? number[].length
numPair.length //? [number, number] length

// numPair.push(6) // [4, 5, 6]
// numPair.pop() // [4, 5]
// numPair.pop() // [4]
// numPair.pop() // []
numPair.push(6) // [4, 5, 6]
numPair.pop() // [4, 5]
numPair.pop() // [4]
numPair.pop() // []

// numPair.length //! ❌ DANGER ❌
numPair.length //! ❌ DANGER ❌

// const roNumPair: readonly [number, number] = [4, 5]
// roNumPair.length
// roNumPair.push(6) // [4, 5, 6] //! Not allowed
// roNumPair.pop() // [4, 5] //! Not allowed
const roNumPair: readonly [number, number] = [4, 5]
roNumPair.length
roNumPair.push(6) // [4, 5, 6] //! Not allowed
roNumPair.pop() // [4, 5] //! Not allowed

/**/

Expand Down
Loading