DEV Community

Pragmatic Maciej
Pragmatic Maciej

Posted on

7

Advanced TypeScript Exercises - Question 7

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 🛑 
}
Enter fullscreen mode Exit fullscreen mode

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 🛑 
Enter fullscreen mode Exit fullscreen mode

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.

Image of Timescale

Timescale – the developer's data platform for modern apps, built on PostgreSQL

Timescale Cloud is PostgreSQL optimized for speed, scale, and performance. Over 3 million IoT, AI, crypto, and dev tool apps are powered by Timescale. Try it free today! No credit card required.

Try free

Top comments (4)

Collapse
 
nombrekeff profile image
Keff •

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

Collapse
 
alextsk profile image
alextsk • • Edited

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

Collapse
 
kpulkit29 profile image
Pulkit Kashyap •

Superb solution :)

Collapse
 
anduser96 profile image
Andrei Gatej •
👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay