Skip to content

Instantly share code, notes, and snippets.

View hoangtranson's full-sized avatar
🎯
Focusing

Hoang Tran Son hoangtranson

🎯
Focusing
View GitHub Profile
menu = ['bun bo hue', 'cha ca', 'pho viet', 'banh xeo']
price = {
"bun bo hue": 30,
"cha ca": 25,
"pho viet": 40,
"banh xeo": 10
}
order = input('please make an order:')
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div class="grandparent">
<!DOCTYPE html>
<html>
<head>
<title>Page Title</title>
</head>
<body>
<div class="grandparent">
// Unions
type NetworkLoadingState = {
state: "loading"; // Discriminant
};
type NetworkFailedState = {
state: "failed"; // Discriminant
code: number;
};
interface Options {
material: string;
backlight: boolean;
}
// Mapped types
type ReadOnly<T> = { readonly [k in keyof T]: T[k]; }
type Optional<T> = {[k in keyof T]?: T[k]; }
type Nullable<T> = {[k in keyof T]: T[k] | null; }
// Creating new types from mapped types
interface Cephalopod {
hasInk: boolean;
arms: number;
tentacles: number;
}
interface CephalopodDictionary {
[index: string]: Cephalopod;
}
let dictionary: CephalopodDictionary = {};
dictionary['octopus vulgaris'] = { hasInk: true, arms: 8, tentacles: 0 };
// Using bracket []
let list: number[] = [1, 2, 3];
// Using Array<elemType>
let list: Array<number> = [1, 2, 3];
interface ErrorHandling {
success: boolean;
error?: { message: string };
}
interface ArtworksData {
artworks: { title: string }[];
}
interface ArtistsData {
// String Literal Types
type Easing = "ease-in" | "ease-out" | "ease-in-out";
class UIElement {
animate(dx: number, dy: number, easing: Easing) {
if (easing === "ease-in") {
// ...
} else if (easing === "ease-out") {
} else if (easing === "ease-in-out") {
// variable
let union: boolean | number;
union = 5; // OK: number
union = true; // OK: boolean
union = 'string'; // Error: Type "string" is not assignable to type 'number | boolean'
// function
function padLeft(value: string, padding: string | number) {
// ...
}