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

narrowing to null via typeguards doesn't work #27634

Closed
zpdDG4gta8XKpMCd opened this issue Oct 9, 2018 · 11 comments
Closed

narrowing to null via typeguards doesn't work #27634

zpdDG4gta8XKpMCd opened this issue Oct 9, 2018 · 11 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@zpdDG4gta8XKpMCd
Copy link

zpdDG4gta8XKpMCd commented Oct 9, 2018

i am hacking my way around this limitation #11920

so my goal is to make sure that isNull only applied to T | null types and by this i mean that null must be a valid case of a given type (types that don't have | null must not be permitted), i am trying to use conditional types to enforce it, this is what i got

unfortunately it doesn't work and i am clueless what i am doing wrong, it looks like a bug

export function isNull<T extends (null extends T ? any : never)>(value: T | null): value is null {
    return value === null;
}

declare var x: null | number;
if (isNull(x)) { // <-- expect to work, actual: number is not assignable to null
}
declare var y: number;
if (isNull(y)) { // <-- expect a type error
}

@ahejlsberg you might want to look at it

@RyanCavanaugh
Copy link
Member

You'd need #9252 to do this

@zpdDG4gta8XKpMCd
Copy link
Author

zpdDG4gta8XKpMCd commented Oct 9, 2018

jfc, no country for an old man

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Oct 9, 2018
@zpdDG4gta8XKpMCd
Copy link
Author

zpdDG4gta8XKpMCd commented Oct 9, 2018

@RyanCavanaugh is there any way to accomplish this up to your knowledge?
i want to flag places that check for null and where null isn't a valid option

@jack-williams
Copy link
Collaborator

Maybe?

export function isNull<T extends (null extends T ? any : never)>(value: T): value is Extract<T,null> {
    return value === null;
}

@zpdDG4gta8XKpMCd
Copy link
Author

Extract<T, null> is sure a very interesting way of saying null, but it seems to work, thanks

@ahejlsberg
Copy link
Member

I was going to suggest this:

export function isNull<T extends (null extends T ? any : never)>(value: T): value is T & null {
    return value === null;
}

But Extract<T, null> seems to work too.

@zpdDG4gta8XKpMCd
Copy link
Author

zpdDG4gta8XKpMCd commented Oct 9, 2018

@ahejlsberg your way (value is T & null) works better since Except<T, null> only works for unions of primitives, but doesn't work on unions of objects, thank you for your help

@jack-williams
Copy link
Collaborator

@Aleksey-Bykov Can you post an example of one that doesn't work?

@zpdDG4gta8XKpMCd
Copy link
Author

@jack-williams hm, i guess i am taking my words back, now it looks working just as well

not sure what happened, but when i applied it first time i didn't get narrowing that i expected

if i see it again i will let you know

@zpdDG4gta8XKpMCd
Copy link
Author

broken now: #29662

@typescript-bot
Copy link
Collaborator

This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

5 participants