-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Allow to use null as discriminator for tagged union #24193
Comments
|
@MartinJohns If Edit: I made a mistake in if-condition, it should be |
@KSXGitHub I think you meant |
@ajafff Oh, thanks. I made a mistake. |
Isn't this the same as #24479? |
Oh whoops, I'm the duplicate. |
Just copy-pasting a use-case from my issue, #25447 , interface A {
f?: number;
bar: string;
}
interface B {
f: number;
}
declare const x: A | B;
if (x.f == undefined) {
//Expected:
//'x' is narrowed to type 'A'
//Actual:
//Property 'bar' does not exist on type 'A | B'.
//Property 'bar' does not exist on type 'B'.
console.log(x.bar);
} |
Seems like this is the feature required to type Using the standard animated import { Spring } from "react-spring";
<Spring
from={{ background: 'blue', width: 0 }}
to={{ background: 'red', width: 100 }}
>
{style => (
<div style={style}>React animation</div>
)}
</Spring> With native (i.e. directly to DOM, skipping react) animation, import { Spring, animated } from "react-spring";
<Spring
native
from={{ background: 'blue', width: 0 }}
to={{ background: 'red', width: 100 }}
>
{style => (
<animated.div style={style}>Native animation</animated.div>
)}
</Spring> At the moment I can type This isn't specific to JSX, though, here's a simplified example of this case: declare class AnimatedValue<T> {
interpolate<U>(map: (value: T) => U): AnimatedValue<U>;
}
type DynamicProps<T> = { native?: false, value: T, render: (value: T) => string };
type NativeProps<T> = { native: true, value: T, render: (value: AnimatedValue<T>) => AnimatedValue<string> };
type AutoProps<T> = DynamicProps<T> | NativeProps<T>;
declare function renderAuto<T>(props: AutoProps<T>): string;
// Works, x is number:
renderAuto({ native: false, value: 123, render: x => x.toFixed() });
// x is any, so error under --noImplicitAny
renderAuto({ /* native: false, */ value: 123, render: x => x.toFixed() });
// works, ax is AnimatedValue<number>
renderAuto({ native: true, value: 123, render: ax => ax.interpolate(x => x.toFixed()) }); |
Accepting PRs or we'll take care of it at some future point |
PR up at #27631 |
Search Terms
Suggestion
Right now, even with
strictNullChecks
is set totrue
,null
cannot be used as a discriminator for tagged union.Use Cases
Recently, I made a pull request for
@types/node
in which I letspawnSync
returns a tagged union witherror
property as discriminator.Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: