Skip to content

Commit 44e4367

Browse files
committed
Objects, Type Aliases, Readonly, & Optional
1 parent 18916d0 commit 44e4367

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

TS/Basics/objects.ts

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Objects
2+
3+
const user = {
4+
name: "Tony",
5+
email: "ironman@avengers.com",
6+
isActive: true
7+
}
8+
9+
// Weird Behavior
10+
// function createUser({name: string, isPaid: boolean}){}
11+
// let newUser = {name: "Peter", isPaid: false, email: "t@t.com"}
12+
// createUser(newUser);
13+
14+
// Better way
15+
function createCourse ():{name: string, price: number}{
16+
return {name: "Full Stack Web Development", price: 599}
17+
}
18+
19+
// Type Aliases
20+
21+
type User = {
22+
name: string;
23+
email: string;
24+
isActive: boolean
25+
}
26+
27+
function createUser(user: User){}
28+
29+
createUser({name: "", email: "", isActive: false})
30+
31+
// READONLY (Scenario Based)
32+
type UserDetails = {
33+
readonly _id: string; // wont be able to change the value of _id down the road
34+
name: string;
35+
email: string;
36+
isActive: boolean;
37+
creditCardDetails?: number // optional (?)
38+
}
39+
40+
let newUser: UserDetails = {
41+
_id: "12345",
42+
name: "Peter",
43+
email: "parker@avengers.com",
44+
isActive: true,
45+
}
46+
47+
type cardNumber = {
48+
cardNumberVal: string
49+
}
50+
51+
type cardDate = {
52+
cardDateValue: string
53+
}
54+
55+
// Using existing types to define another type
56+
type cardDetails = cardNumber & cardDate & {
57+
cvv: number
58+
}
59+
60+
newUser.email = "pete@avengers.com";
61+
// newUser._id = "234"; // Cannot assign to '_id' because it is a read-only property.
62+
63+
64+
export {}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy