Beginner
A getProp that can't be wrong
Write a generic function getProp(obj, key) that returns the property called key. The key must be a real key of obj, and the return type must be the type of that property.
Use <T, K extends keyof T> and the return type T[K]. Don't use any.
What Check tests
- Your code has no type errors
- The console prints
Dune 412 - The result of
getProp(book, "pages")is anumber - The result of
getProp(book, "title")is not anumber - A key that isn't on the object is rejected
- No
anyin your code
Stuck? One hint at a time
The solution unlocks after your first Check. Try first: that is where the learning happens.
Runs in your browser. Your code is saved here.
Press Run to see the page.
Console
Nothing printed yet.