Reputation: 4333
I have a complicated generic type IsInteresting<T>
that becomes either the type true
or false
depending on what T
I give it:
type IsInteresting<T> = (/* ... */) ? true : false;
Now I want to create a function that receives as first argument an interesting value, i.e., a value whose type U
would cause IsInteresting<U>
to be true
. How can I accomplish this?
const operateOnInterestingValue: WhatFunctionTypeDoIPutHere = value => {
// ...
};
let a: T1; // Assume `IsInteresting<T1>` would give `true`
let b: T2; // Assume `IsInteresting<T2>` would give `false`
operateOnInterestingValue(a); // Must compile
operateOnInterestingValue(b); // Must not compile
Upvotes: 0
Views: 27
Reputation: 1222
If you slightly change IsInteresting<T>
this way:
type IsInteresting<T> = (/* ... */) ? T : never;
You can then type your function like this:
const operateOnInterestingValue = <T>(value: IsInteresting<T>) => {
// ...
};
The compiler will complain if you try to call operateOnInterestingValue
with a variable whose IsInteresting<type>
would resolve to never
.
Upvotes: 1