Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

assertType throws errors on valid union null results #73

Open
stevenlaidlaw opened this issue Aug 13, 2020 · 6 comments
Open

assertType throws errors on valid union null results #73

stevenlaidlaw opened this issue Aug 13, 2020 · 6 comments

Comments

@stevenlaidlaw
Copy link

const body: string | null = assertType<string | null>(null);

This simple test will cause a TypeGuardError to be thrown.

@Pwuts
Copy link

Pwuts commented Sep 20, 2020

A similar problem exists with null values in objects:

type test1 = {
    a?: number,
    b: string
};
type test2 = {
    a: null | number,
    b: string
};

const testObject1 = {
    a: null,
    b: 'asdf'
};
const testObject2 = {
    b: 'asdf'
};

console.log(is<test1>(testObject1), is<test1>(testObject2));
console.log(is<test2>(testObject1), is<test2>(testObject2));

output:

false true
false false

should be (as far as I know):

true true
true false

@woutervh-
Copy link
Owner

hi @stevenlaidlaw and @Pwuts

I'm suspecting it has to do with the strictNullChecks option. Can you provide me with your compiler options / tsconfig.json?

@woutervh-
Copy link
Owner

I've made a preliminary fix in v0.17.0 which hopefully addresses the | null issue with strictNullChecks: false.
https://github.com/woutervh-/typescript-is/releases/tag/v0.17.0

Please let me know if it's working or not.

@ff-jkrispel
Copy link

it worked 🎉

@Pwuts
Copy link

Pwuts commented Dec 6, 2020

@woutervh- in reference to my previous comment:

with "strictNullChecks" omitted from my tsconfig.json:

false true
false false

with "strictNullChecks": true:

false true
true false

with "strictNullChecks": false:

true true
true false

The default of strictNullChecks is false (source), so the first and last result should be the same, right?

Some other possibly notable options from my tsconfig.json:

"target": "ES2020",
"allowSyntheticDefaultImports": true,
"lib": ["ES2020", "dom"],

@Pwuts
Copy link

Pwuts commented Dec 29, 2020

(Sidenote: I looked into fixing this myself and making a PR, but there was so much code and it seemed immensely complex; I didn't even know where to begin. If you can point me in a direction I might be able to contribute.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants