Created
September 6, 2025 16:26
-
-
Save davidystephenson/ca7090ee54913efdcc8d5bb3456eb05d 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
interface Tree { | |
branches: number | |
species: string | |
} | |
interface Vehicle { | |
position: number | |
speed: number | |
} | |
interface Animal { | |
legs: number | |
speed: number | |
} | |
const oak = { | |
branches: 10, | |
species: 'oak' | |
} | |
const willow = { | |
branches: 100, | |
species: 'willow' | |
} | |
const scionIq: Vehicle = { | |
position: 0, | |
speed: 80 | |
} | |
function describeTree<K extends keyof Tree>(tree: Tree, key: K) { | |
const value = tree[key] | |
const message = `The ${key} of this tree is ${value}` | |
console.log(message) | |
} | |
describeTree<'branches'>(oak, 'branches') | |
function describeWithSpeed <T extends { speed: number }> (t: T, key: keyof T & string) { | |
const value = t[key] | |
const message = `The ${key} is ${value}, and the speed is ${t.speed}` | |
console.log(message) | |
} | |
describeWithSpeed(oak, 'branches') | |
describeWithSpeed(scionIq, 'position') | |
const cheetah: Animal = { legs: 4, speed: 90 } | |
describeWithSpeed(cheetah, 'legs') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment