Last active
November 10, 2024 08:39
-
-
Save bradtraversy/f80a4cd87e7034bea5264f7d8c431b4e to your computer and use it in GitHub Desktop.
Basic intro to TypeScript (From YouTube Crash Course)
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
// 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 |
THANK YOU SO MUCH LEARNED A LOT!
Thanks Brad 👌🏽
thanks
Thanks, Brad!
Thanks, Brad!!! 🙌🏽
best TS review cheatsheet
line 15 is employees
thanks
Thank you!
Thank you!
Thanks Brad
Thanks fot he course man!
Thanks Brad
Thank you brother, very helpfull
Good Work Brad!
thank you
thanks, very helpful!
2023/05/06 I was here . Thanks
Thank you
7.21.23 # 9.13AM
thank you!!
its a good way to start ts!!!!Thank you brad
Thanks aLOT Brad!
Thanks a lot
Thank you... 👍
Thank you Brad! appreciate your work
Thank you!!
cool
Thanks! Really useful video and gist.
Thank you
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
KEEP UP THE GREAT WORK