Created
February 15, 2026 19:39
-
-
Save davidystephenson/a31e6177baefc19ed9de8859ea9bc0d6 to your computer and use it in GitHub Desktop.
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 Input <T> = { | |
| data: T | string | |
| } | |
| type PremiumInput <T> = Input<T> & { | |
| price: number | |
| } | |
| */ | |
| interface Input <T = boolean> { | |
| data: T | string | |
| } | |
| interface PremiumInput <T extends string | number> extends Input<T> { | |
| price: number | |
| } | |
| const alpha: Input<200 | 201> = { data: 200 } | |
| const beta: Input<400 | 404> = { data: 400 } | |
| const gamma: Input = { data: true } | |
| const delta: PremiumInput<number> = { data: 'hello', price: 100 } | |
| function annotate <T> (data: T) { | |
| return { | |
| data, | |
| date: new Date() | |
| } | |
| } | |
| const annotatedString = annotate('hello') | |
| const annotatedNumber = annotate(100) | |
| function formatNumber (value: number) { | |
| console.log(value.toFixed(2)) | |
| } | |
| formatString(annotatedString.data) | |
| formatNumber(annotatedNumber.data) | |
| interface Artist { | |
| name: string | |
| medium: string | |
| works: string[] | |
| } | |
| type ArtistKey = keyof Artist | |
| type ArtistValue = Artist[ArtistKey] | |
| const dorothy: Artist = { | |
| name: 'Dorothy Parker', | |
| medium: 'plays', | |
| works: [] | |
| } | |
| function describeArtist <T extends ArtistKey> (artist: Artist, key: T) { | |
| const value = artist[key] | |
| console.log(`The ${key} of ${artist.name} is ${value}`) | |
| return value | |
| } | |
| const medium = describeArtist(dorothy, 'medium') // The medium of Dorothy Parker is plays | |
| const works = describeArtist(dorothy, 'works') | |
| function formatString (value: string){ | |
| console.log(value.toLowerCase()) | |
| } | |
| function formatArray (value: string[]) { | |
| console.log(value.join(', ')) | |
| } | |
| formatString(medium) | |
| formatArray(works) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment