I'd like to know if it's possible to extend a type depending on another type's value?
interface NavType {
hideNav?: boolean
dev?: boolean
}
I'd like to allow the use of hideNav
only if the dev
arg is false.
const Navbar: React.FC<NavType> = ({ children, hideNav, dev = false }) => (
<nav>
{!dev ? (
<NavDefault hideNav={!!hideNav}>{children}</NavDefault>
) : (
<NavDev>{children}</NavDev>
)}
</nav>
)
How can I use conditionals in this case?
// Should PASS
const navArgs1: NavType = {
hideNav: undefined,
dev: true
}
const navArgs2: NavType = {
hideNav: true or false,
dev: false
}
// Should NOT PASS
const navArgs3: NavType = {
hideNav: true or false,
dev: true
}
To extend types in TypeScript, we can use the extends keyword. to create the UserEvent interface that extends the Event type. We extend it by adding the string UserId field in UserEvent and inheriting the rest from Event .
Syntax: We can create the conditional types with the used of ternary operator and extends in TypeScript. Type1 extends Type2 ? for One value : for different value; Here extend works as compare function which checks is Type1 have properties of Type2 if yes then it jumps to true branch else in the false branch.
What does ?: mean in TypeScript? Using a question mark followed by a colon ( ?: ) means a property is optional. That said, a property can either have a value based on the type defined or its value can be undefined .
You can define 2 types and let the component properties be their union; in principle the following (shown as TS Playground code):
interface NavBase {
dev: boolean;
}
interface DevNavType extends NavBase {
dev: true;
hideNav?: undefined;
}
interface NotDevNavType extends NavBase {
dev: false;
hideNav: boolean;
}
// This simulates your component
function f(x: DevNavType | NotDevNavType) {
}
f({ dev: true }); // Works
f({ dev: true, hideNav: undefined }); // Also works
f({ dev: false, hideNav: true }); // Works too - for any boolean value of hideNav
f({ dev: false }); // Compiler error
f({ dev: true, hideNav: true }); // Compiler error
Notes:
NavBase
type NavType = DevNavType | NotDevNavType
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With