Last active
January 15, 2022 04:38
-
-
Save obaranovskyi/b38690fa6fc481d6592a6c41788fe9a9 to your computer and use it in GitHub Desktop.
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
const email = 'john@gmail.com'; // type 'john@gmail.com' | |
const phones = [8494922901, 1238399293]; // type number[] | |
const session = { id: 123, name: 'x1keck323jKJdf1' }; | |
// type | |
// { | |
// id: number; | |
// name: string; | |
// } | |
const username = 'John' as const; // type 'John' | |
const roles = [ 'read', 'write'] as const; // type readonly ["read", "write"] | |
const address = { street: 'Tokyo', country: 'Japan' } as const; | |
// type | |
// { | |
// readonly street: "Tokyo"; | |
// readonly country: "Japan"; | |
// } | |
const user = { | |
email, | |
phones, | |
session, | |
username, | |
roles, | |
address | |
} as const; | |
// { | |
// readonly email: "john@gmail.com"; | |
// readonly phones: number[]; | |
// readonly session: { | |
// id: number; | |
// name: string; | |
// }; | |
// readonly username: "John"; | |
// readonly roles: readonly ["read", "write"]; | |
// readonly address: { | |
// readonly street: "Tokyo"; | |
// readonly country: "Japan"; | |
// }; | |
// } | |
// With const assertion | |
// user.email = 'jim@gmail.com' // Error | |
// user.phones = []; // Error | |
user.phones.push(859324293); | |
// user.session = { name: 'new session name', id: 124 }; // Error | |
user.session.name = 'new session name'; | |
// With const assertion + inner const assertion | |
// user.username = 'Jim'; // Error | |
// user.roles.push('ReadAndWrite'); // Error | |
// user.address.city = 'Osaka'; // Error |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment