Skip to content

Commit 3fe9535

Browse files
add notes
1 parent d792471 commit 3fe9535

File tree

1 file changed

+46
-6
lines changed

1 file changed

+46
-6
lines changed

src/index.ts

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,19 @@ const username: UserName = {
210210
name: 'Justin',
211211
}
212212

213+
// Access Modifiers - this is a way to make properties of an object private or public
214+
// public - this is the default
215+
// private - this is only accessible within the class
216+
// protected - this is only accessible within the class and any subclasses
217+
// readonly - this is a property that can only be read, not changed
218+
219+
// Type Aliases - this is a way to create a new type
220+
type ID = string
221+
type PopularTag = string
222+
type MaybePopularTag = PopularTag | null
223+
let popularTags: PopularTag[] = ['dragon', 'coffee']
224+
let dragonsTag: MaybePopularTag = 'dragon'
225+
213226
// Type Assertion - this is a way to tell the compiler that you know better than it does what the type of a variable is
214227
let cid: any = 1
215228
// let customerId = <number>cid // this is the old way
@@ -254,9 +267,29 @@ const user1: UserInterface = {
254267
id: 1,
255268
name: 'Justin',
256269
}
257-
258270
// user1.id = 5 // this will not work because id is read only
259271

272+
// Interfaces with classes - this is a way to define a type for a class that can be reused in multiple places
273+
interface TakePhoto {
274+
cameraMode: string
275+
filter: string
276+
burst: number
277+
}
278+
// this is a class that implements the TakePhoto interface
279+
class Instagram implements TakePhoto {
280+
cameraMode = 'auto'
281+
filter = 'normal'
282+
burst = 3
283+
}
284+
// this is a class that implements the TakePhoto interface using a constructor
285+
class YouTube implements TakePhoto {
286+
constructor(
287+
public cameraMode: string,
288+
public filter: string,
289+
public burst: number,
290+
) {}
291+
}
292+
260293
// Interfaces with functions - this is a way to define a type for a function that can be reused in multiple places
261294
interface MathFunc {
262295
(x: number, y: number): number
@@ -265,6 +298,13 @@ interface MathFunc {
265298
const add: MathFunc = (x: number, y: number): number => x + y
266299
const subtract: MathFunc = (x: number, y: number): number => x - y
267300

301+
// Abstract Classes - this is a way to define a type for a class that can be reused in multiple places
302+
abstract class TakePhoto {
303+
constructor(public cameraMode: string, public filter: string) {}
304+
}
305+
// this is a class that extends the TakePhoto abstract class
306+
class TikTok extends TakePhoto {}
307+
268308
// Union - this is a variable that can be multiple types but you can only assign one type
269309
let pid: string | number = 2
270310

@@ -326,13 +366,13 @@ class Employee extends Person {
326366

327367
const emp = new Employee(3, 'Jupiter', 25, 'CEO') // this is an instance of the class
328368

329-
// console.log(justin) // this will work because id and name are public
330-
// console.log(john) // this will work because id and name are public
331-
// console.log(justin.id) // this will work because id is public
332-
// console.log(justin.name) // this will work because name is public
369+
console.log(justin) // this will work because id and name are public
370+
console.log(john) // this will work because id and name are public
371+
console.log(justin.id) // this will work because id is public
372+
console.log(justin.name) // this will work because name is public
333373
// console.log(justin.age) // this will not work because age is private
334374
console.log(justin.register()) // this will work because register is public
335-
// console.log(employee)
375+
console.log(employee)
336376
console.log(emp.name) // this will work because name is public
337377

338378
// Generics - this is a way to make a function reusable for multiple types of data (this is a function that takes a string and returns a string)

0 commit comments

Comments
 (0)