Skip to content

Instantly share code, notes, and snippets.

View obaranovskyi's full-sized avatar

Oleh Baranovskyi obaranovskyi

View GitHub Profile
/** @deprecated use User class instead */
class OldUser {}
class User {
/** @deprecated */
constructor(public username: string, public age: number) {}
/** @deprecated */
static createUser(username: string, age: number) {
class User {
username: string;
age: number;
constructor(username: string,age: number) {
this.username = username;
this.age = age;
}
static [propName: string]: string | number;
class User {
username: string;
age: number;
constructor(username: string,age: number) {
this.username = username;
this.age = age;
}
[propName: string]: string | number;
function userCount() {
/* Mock data */
return 15;
};
class User {
id = 0;
static count: number = 0;
constructor(
{
"compilerOptions": {
"noImplicitOverride": true
}
}
class Employee {
doWork() {
console.log('I\'m working!');
}
}
class Developer extends Employee {
override doWork() {
console.log('I\'m programming!')
}
const roles = ['read', 'write', 'readAndWrite'] as const;
type Roles = typeof roles[number];
// equals to this
// type Roles = "read" | "write" | "readAndWrite"
// or even like this
type RolesInCapital = Capitalize<typeof roles[number]>;
// equals to this
// type RolesInCapital = "Read" | "Write" | "ReadAndWrite"
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'
function valueCheck(value: string | number) {
if (typeof value === "string") {
// value has type string
} else if (typeof value === "number") {
// value has type number
} else {
value; // value has type never
}
}
function endlessWhile(): never {
while(true) {
/* ... */
}
}