1

I have some complex types:

type odds: 1 | 3 | 5 | 7 | 9; type evens: 2 | 4 | 6 | 8 | 0 

...and some function which takes those complex types:

function(digit: odds | evens) { ... } 

I would like to check which type I'm getting, but none of the following work:

if (digit isntanceof odds) // error: odds refers to a type but is being used as a value if (typeof digit === ???) // issue: no single value for typeof 

.

How can I go about checking if digit is odd using types?

2 Answers 2

3

Types don't exist at compiler time so typeof will not work, you need some other type of check to test the type at runtime. Typescript has support for this using a custom type-guards

type odds = 1 | 3 | 5 | 7 | 9; type evens = 2 | 4 | 6 | 8 | 0; function isOdd(v: odds | evens) : v is odds { return v % 2 != 0 } declare let n: odds | evens; withEven(n) // invalid here no check has been performed withOdd(n) // invalid here no check has been performed if (isOdd(n)) { n // is typed as odd withEven(n) // invalid here withOdd(n) // valid here we checked it is odd } else { n // is typed as even withEven(n) // valid here we checked it is not odd withOdd(n) // invalid here } function withEven(n: evens) { } function withOdd(n: odds) { } 
Sign up to request clarification or add additional context in comments.

4 Comments

so assume the code v % 2 != 0 can not exist, nor any other form of even number finding arithmetic, how would this work? Can it just return true because v is odds has already asserted it must be odd. And if there is no assertion that it's odd, what does v is odds do to help?
@SephReed If you post a more realistic example the solution will be better. You need to have some sort of runtime test that will return true or false. There is no way to have runtime behavior be directed by a type since the type does not exist at runtime (a class is a different discussion but types are erased at compile time)
You need to have some sort of runtime test that will return true or false is a fair answer.
@SephReed The benefit is the type will be narrowed inside the each branch of the if n is typed as odd in the then of the if and is typed as event in the else. This allows you to access fields based on the narrowed type or pass n to a function expecting an even number in then but not in else
0

@SephReed Union types in TS provide a way to check the property. You can create the types and then do a check on types property. Please check the advance types with union http://www.typescriptlang.org/docs/handbook/advanced-types.html

interface Evens { evenProp:number; 

}

interface Odds { oddProp:number; 

}

class AdvanceUnionType{ check = function(digit: Odds | Evens) { if ((<Odds>digit).oddProp){ console.log("Odd object passed"); }else if((<Evens>digit).evenProp){ console.log("Even object passed"); } } 

}

(function(){ 'use strict'; let even:Evens = {evenProp: 10}; let odd:Odds = {oddProp: 9}; let advanceType = new AdvanceUnionType(); advanceType.check(even); advanceType.check(odd); 

}());

2 Comments

Why my reputation is decreased for this response? Whosoever did it can you please put a comment, why did you do it?
your indentation is borked

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.