Skip to content

Instantly share code, notes, and snippets.

@bradtraversy
Last active November 10, 2024 08:39
Show Gist options
  • Save bradtraversy/f80a4cd87e7034bea5264f7d8c431b4e to your computer and use it in GitHub Desktop.
Save bradtraversy/f80a4cd87e7034bea5264f7d8c431b4e to your computer and use it in GitHub Desktop.
Basic intro to TypeScript (From YouTube Crash Course)
// Basic Types
let id: number = 5
let company: string = 'Traversy Media'
let isPublished: boolean = true
let x: any = 'Hello'
let ids: number[] = [1, 2, 3, 4, 5]
let arr: any[] = [1, true, 'Hello']
// Tuple
let person: [number, string, boolean] = [1, 'Brad', true]
// Tuple Array
let employees: [number, string][]
employee = [
[1, 'Brad'],
[2, 'John'],
[3, 'Jill'],
]
// Union
let pid: string | number
pid = '22'
// Enum
enum Direction1 {
Up = 1,
Down,
Left,
Right,
}
enum Direction2 {
Up = 'Up',
Down = 'Down',
Left = 'Left',
Right = 'Right',
}
// Objects
type User = {
id: number
name: string
}
const user: User = {
id: 1,
name: 'John',
}
// Type Assertion
let cid: any = 1
// let customerId = <number>cid
let customerId = cid as number
// Functions
function addNum(x: number, y: number): number {
return x + y
}
// Void
function log(message: string | number): void {
console.log(message)
}
// Interfaces
interface UserInterface {
readonly id: number
name: string
age?: number
}
const user1: UserInterface = {
id: 1,
name: 'John',
}
interface MathFunc {
(x: number, y: number): number
}
const add: MathFunc = (x: number, y: number): number => x + y
const sub: MathFunc = (x: number, y: number): number => x - y
interface PersonInterface {
id: number
name: string
register(): string
}
// Classes
class Person implements PersonInterface {
id: number
name: string
constructor(id: number, name: string) {
this.id = id
this.name = name
}
register() {
return `${this.name} is now registered`
}
}
const brad = new Person(1, 'Brad Traversy')
const mike = new Person(2, 'Mike Jordan')
// Subclasses
class Employee extends Person {
position: string
constructor(id: number, name: string, position: string) {
super(id, name)
this.position = position
}
}
const emp = new Employee(3, 'Shawn', 'Developer')
// Generics
function getArray<T>(items: T[]): T[] {
return new Array().concat(items)
}
let numArray = getArray<number>([1, 2, 3, 4])
let strArray = getArray<string>(['brad', 'John', 'Jill'])
strArray.push(1) // Throws error
@Saurabhdaswant
Copy link

KEEP UP THE GREAT WORK

@Saurabhdaswant
Copy link

THANK YOU SO MUCH LEARNED A LOT!

@panquequelol
Copy link

Thanks Brad 👌🏽

@WayneA330
Copy link

thanks

@surafell
Copy link

Thanks, Brad!

@nadiemedicejose
Copy link

Thanks, Brad!!! 🙌🏽

@andishe-wpd
Copy link

best TS review cheatsheet

@andishe-wpd
Copy link

line 15 is employees

@stalinwesley
Copy link

thanks

@tcodes27
Copy link

tcodes27 commented Oct 3, 2022

Thank you!

@k-zehnder
Copy link

Thank you!

@elidvenega
Copy link

Thanks Brad

@andreyanez
Copy link

Thanks fot he course man!

@segunajibola
Copy link

Thanks Brad

@blackinitial
Copy link

Thank you brother, very helpfull

@waelnassaf
Copy link

Good Work Brad!

@ASWAN123
Copy link

thank you

@mfahriferdiansyah
Copy link

thanks, very helpful!

@Pinche-Dev
Copy link

2023/05/06 I was here . Thanks

@Abu-Muhammed
Copy link

Thank you
7.21.23 # 9.13AM

@abdou251
Copy link

thank you!!

@afzal123afzal
Copy link

its a good way to start ts!!!!Thank you brad

@subha-glitch
Copy link

Thanks aLOT Brad!

@sajith-silverlineit
Copy link

Thanks a lot

@sanchaman1994
Copy link

Thank you... 👍

@sajithnsilvame
Copy link

Thank you Brad! appreciate your work

@Hannah-Moon
Copy link

Thank you!!

@KevinRebakure
Copy link

cool

@patrickgilmour
Copy link

Thanks! Really useful video and gist.

@mdsnn
Copy link

mdsnn commented Oct 19, 2024

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment