loading...

Advanced TypeScript Exercises - Question 7

macsikora profile image Maciej Sikora ・2 min read

Advanced TypeScript Exercises (20 Part Series)

1) Advanced TypeScript Exercises - Question 1 2) Advanced TypeScript Exercises - Answer 1 3 ... 18 3) Advanced TypeScript Exercises - Question 2 4) Advanced TypeScript Exercises - Answer 2 5) Advanced TypeScript Exercises - Question 3 6) Advanced TypeScript Exercises - Answer 3 7) Advanced TypeScript Exercises - Question 4 8) Advanced TypeScript Exercises - Answer 4 9) Advanced TypeScript Exercises - Question 5 10) Advanced TypeScript Exercises - Answer 5 11) Advanced TypeScript Exercises - Question 6 12) Advanced TypeScript Exercises - Answer 6 13) Advanced TypeScript Exercises - Question 7 14) Advanced TypeScript Exercises - Answer 7 15) TypeScript Exercises Bonus🦠 - Type of Pandemia 16) TypeScript Exercises Bonus🦠 - Answers Part 1 17) TypeScript Exercises Bonus🦠 - Answers Part 2 18) Advanced TypeScript Exercises - Question 8 19) Advanced TypeScript Exercises - Answer 8 20) Advanced TypeScript Exercises - Question 9

Today's question will be about creating exclusiveness behavior of the type. TypeScript is structurally typed language, very important is to mention that also types compatibility in TypeScript is based on structural subtyping. That property is visible during assignment and during passing of arguments to functions. Short explanation would be - we can assign or pass values which have at least the same members as wanted type, but also they can have more constraints. More about that in TS documentation.

Our task is to make types more strict and block possibility of passing/assigning subtypes.

7.1 Make a type which will only allow for assignment of an empty object

type EmptyObject = {} // empty object only, 🔥 change the type to be exclusive for any field 

// test cases
const shouldPass: EmptyObject = {}; // this should be ok 🟢
const shouldFail: EmptyObject = {
    prop: 1 // here we should have compile error 🛑 
}

7.2 Change function type to be exclusive for its arguments

type SomeType =  {
    prop: string
}
// change below function type definition 🔥 in order to allow only strict SomeType value
function takeSomeTypeOnly(x: SomeType) { return x }

// test cases
const x = { prop: 'a' };
takeSomeTypeOnly(x) // this should be ok 🟢

const y = { prop: 'a', addditionalProp: 'x' };
takeSomeTypeOnly(y) // here we should have compile error 🛑 

The questions and ready to start code is available in The Playground

Post your answers in comments (preferred links to the playground). Have fun! Answer will be published soon!

This series will continue. If you want to know about new exciting questions from advanced TypeScript please follow me on dev.to and twitter.

Advanced TypeScript Exercises (20 Part Series)

1) Advanced TypeScript Exercises - Question 1 2) Advanced TypeScript Exercises - Answer 1 3 ... 18 3) Advanced TypeScript Exercises - Question 2 4) Advanced TypeScript Exercises - Answer 2 5) Advanced TypeScript Exercises - Question 3 6) Advanced TypeScript Exercises - Answer 3 7) Advanced TypeScript Exercises - Question 4 8) Advanced TypeScript Exercises - Answer 4 9) Advanced TypeScript Exercises - Question 5 10) Advanced TypeScript Exercises - Answer 5 11) Advanced TypeScript Exercises - Question 6 12) Advanced TypeScript Exercises - Answer 6 13) Advanced TypeScript Exercises - Question 7 14) Advanced TypeScript Exercises - Answer 7 15) TypeScript Exercises Bonus🦠 - Type of Pandemia 16) TypeScript Exercises Bonus🦠 - Answers Part 1 17) TypeScript Exercises Bonus🦠 - Answers Part 2 18) Advanced TypeScript Exercises - Question 8 19) Advanced TypeScript Exercises - Answer 8 20) Advanced TypeScript Exercises - Question 9

Posted on Mar 11 by:

macsikora profile

Maciej Sikora

@macsikora

I am Software Developer, currently interested in static type languages (TypeScript, Elm, Reason) mostly in the frontend land

Discussion

markdown guide
 

I got it, took me a while, here is the playground.

Cool exercise! I didn't know about some of the stuff I used in the solution :P

 
 

Thanks for making me read the docs(at last))
playground