0

EDIT: The problem was simply not using strict mode, which correctly disallowed the types with null properties and correctly inferred both true and false cases.

I'm writing a discriminated union where the tag is a boolean, but trying to use !x.tag as a type guard doesn't work. I suspected the reason is because a falsey value like null or undefined would also count, but defining the tag type as NonNullable<true> and NonNullable<false> didn't help -- I'm still able to create an object of type isTrue or isFalse with tag: null. This would make it fail the type guard even in the truthy case, so I don't understand what I'm missing. Example code:

// definitions:

interface isTrue {
    tag: NonNullable<true>;
    data: string;
}

interface isFalse {
    tag: NonNullable<false>;
    data: number;
}

type either = isTrue | isFalse;


// playing around:

const falsy: isFalse = { tag: null, data: 5 }; // would expect this not to be ok but it is

const truthy: isTrue = { tag: null, data: 'a' }; // this is also allowed

decider({ tag: null, data: 4 }) // a legal call since the argument is valid as a isFalse object.

function decider(x: either) {
    if (x.tag) { // inferred as isTrue
        x.data
        x.tag
    } else { // still a union
        x.data
        x.tag
    }
}

I've also tried putting !x.tag in the if clause and leaving the true case to the else clause, but in both cases the true clause is inferred while the false one isn't.

Ran Lottem
  • 416
  • 5
  • 14
  • 1
    [Can't reproduce the issue](http://www.typescriptlang.org/play/#code/JYOwLgpgTgZghgYwgAmAZwCpQK4oN4BQyxyYcA5gFzIByA9iDdgDbNwBGzEAPGDhAD4A3ERIATOGWpo+ociIC+BUJFiIU6AGJxmafKOJkqtBk1Ycu3eLsEiSyCVOQhsAW3bRFBMAE8ADigQwGAAFtDIALyomPzIAD7R2jYiKtDwSNEA6nRQYsiE9kbUAOQA7jlixXbiknDSsiDyBEoECAwyyNZoPtRaOnqR+aQU1C6sADQOtdQArMgKIq3tYKQ4oT3RWLiDeMPGY8yTjnXIxXDF84tiEAjA11AAFLtFziyHU04ALPMAlIsw2BACDAwAYDhud2gDwAHtQgqFoD98gZUDBkDCAHRGJEFewkaEY47VPHIAlGYnzZAQGzIknEAlElH2MkUClKBRAA) – Nino Filiu Jul 09 '19 at 19:47
  • true, I compared flags and the matter stemmed from not being in strict mode. – Ran Lottem Jul 09 '19 at 19:54
  • 1
    So, problem resolved then? – Nino Filiu Jul 09 '19 at 19:55
  • 1
    Definitely, just not sure if etiquette is to leave it up in case anyone comes up with a similar issue or to delete. – Ran Lottem Jul 09 '19 at 19:56
  • 1
    The etiquette is to close as "problem can't be reproduced, or was caused by a typo" – Nino Filiu Jul 09 '19 at 19:57
  • I can't find that option anywhere, care to explain? – Ran Lottem Jul 09 '19 at 20:00
  • 1
    Click on the close question link at the bottom of the question, then select `off topic > reason #5` – Nino Filiu Jul 09 '19 at 20:15

0 Answers0