-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path49.ConstAssertion.ts
51 lines (41 loc) · 946 Bytes
/
49.ConstAssertion.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Const Assertions
const king = 'King Arthur'
king = 'Arthur, King of the Britons'; // error
const upperCaseKing = king.toUpperCase(); // KING ARTHUR
const dave = {
name : 'Dave',
role: 'drummer',
skills: ['drums']
}
dave ={
name: 'David',
role: 'guitarist',
skills: ['guitar']
} // error
dave.name = 'David'; // OK
dave.role = 'guitarist'; // OK
dave.skills = ['guitar']; // OK
const jane = {
name: 'Jane',
role: 'singer',
skills: ['vocals']
} as const;
jane.name = 'Janet'; // error
jane.role = 'lead singer'; // error
jane.skills = ['dance']; // error
function layout(settings:{
align: 'left' | 'right' | 'center'
padding: number
}){
console.log('Performing layout with settings: ' + JSON.stringify(settings));
}
const example = {
align: 'left',
padding: 1
}
const example2 = {
align: 'left' as const,
padding: 1
}
layout(example); // ERROR
layout(example2); // OK