This file contains hidden or 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
from typing import TypedDict | |
class Person(TypedDict): | |
name: str | |
age: int | |
def greet(person: Person) -> str: | |
return f"Hello, {person['name']}!" |
This file contains hidden or 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
interface Person { | |
name: string; | |
age: number; | |
} | |
function greet(person: Person): string { | |
return `Hello, ${person.name}!`; | |
} |
This file contains hidden or 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
// 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 = { |
This file contains hidden or 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
type TShirt = { color: string, size: 'S' | 'M' | 'L', brand: string} |
This file contains hidden or 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
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; | |
} |
This file contains hidden or 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
function addTwoNumbers(firstNumber: number, secondNumber: number) { | |
return firstNumber + secondNumber; | |
} |
This file contains hidden or 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
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; | |
} |
This file contains hidden or 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
interface SimpleHouse { | |
price: number; | |
color: string; | |
numberOfWindows: 0 | 1 | 2; | |
hasChimney: boolean; | |
} |