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
interface Person { | |
name: string | |
address?: { | |
city: string | |
zipcode: string | |
} | |
} | |
const person: Person = { name: "John", address: undefined } |
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
interface Person { | |
name: string | |
address?: { | |
city: string | |
zipcode: string | |
} | |
} | |
const p: Person = { name: "John", address: undefined } |
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
interface Person { | |
name: string | |
address?: { | |
city: string | |
zipcode: string | |
} | |
} | |
const p: Person = { name: "John" } |
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
let myPrices: number[] = [1, 2.4, 2.0] | |
const defaultPrice = myPrices[40] | |
// `defaultPrice` is of type `number | undefined` |
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
let myPrices: number[] = [1, 2.4, 2.0] | |
const defaultPrice = myPrices[40] | |
// `defaultPrice` is of type `number` |
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
function isPremiumUser(user: User | PremiumUser): user is PremiumUser { | |
return user.plan === "premium" | |
} | |
let user: PremiumUser | User | undefined | |
// the line below should raise a TypeScript error, | |
// but it doesn't! | |
if (isPremiumUser(user)) { | |
// ... |
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
interface Success { | |
type: `${string}Success`; | |
body: string; | |
} | |
interface Error { | |
type: `${string}Error`; | |
message: string; | |
} |
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
type UserArg = PremiumUser | USer | |
function processUSer({ plan, premiumOptions }: UserArg) { | |
if (plan === "premium") { | |
premiumOptions | |
// `premiumOptions` is of type `PremiumOptions` | |
} else { | |
premiumOptions | |
// `premiumOptions` is of type `undefined` | |
} |
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
type UUID = string; | |
interface BasicUser { | |
id: UUID; | |
first_name: string; | |
last_name: string; | |
} | |
interface User extends BasicUser { | |
plan: 'free' |
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
import { isString } from "lodash" | |
let name: string | undefined = "John" | |
// `name` is of type `string | undefined` | |
let price: number | undefined = 1 | |
// `price` is of type `number | undefined` | |
if (isString(name)) { | |
name |
NewerOlder