Skip to content

Instantly share code, notes, and snippets.

from typing import TypedDict
class Person(TypedDict):
name: str
age: int
def greet(person: Person) -> str:
return f"Hello, {person['name']}!"
interface Person {
name: string;
age: number;
}
function greet(person: Person): string {
return `Hello, ${person.name}!`;
}
// 1) create a type, and name it TShirt
type TShirt = {
color: string;
size: 'small' | 'medium' | 'large'; // this is a union, meaning the size could be *one* of these values
brand: string;
};
// 2) create a variable and identify it as a TShirt. Then assign the
// object literal to that variable.
const largeBlueTShirt: TShirt = {
type TShirt = { color: string, size: 'S' | 'M' | 'L', brand: string}
interface GuestUser {
/** the user's unique id */
id: string;
/** with this modification, now the role can be 'guest' or any number value such as 123, or 987437
role: 'guest' | number;
// or, even more specifically, if you didn't want just any number, only 78
role: 'guest' | 78;
}
@conraddavisjr
conraddavisjr / addTwoNumbers.ts
Created September 8, 2022 00:29
The Typescript Guide I wish I had - Reading Errors, Part 1 - addTwoNumbers snippet
function addTwoNumbers(firstNumber: number, secondNumber: number) {
return firstNumber + secondNumber;
}
@conraddavisjr
conraddavisjr / interface_SimpleHouse_comments.ts
Last active September 7, 2022 15:56
The Typescript Guide I wish I had - interface - SimpleHouse with comments
interface SimpleHouse {
price: number; // 10000;
color: string; // any color. If it's a string, it can be any name
numberOfWindows: 0 | 1 | 2; // it can be 0, 1, or 2
hasChimney: boolean; // true;
}
@conraddavisjr
conraddavisjr / interface_SimpleHouse.ts
Last active September 7, 2022 15:51
The Typescript Guide I wish I had - interface - SimpleHouse
interface SimpleHouse {
price: number;
color: string;
numberOfWindows: 0 | 1 | 2;
hasChimney: boolean;
}