-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
JSDoc type is not narrowing a union (but typescript does) #61339
Comments
The typescript version has strict null checks enabled. The javascript version does not. Enable strict null checks in the javascript version and the error goes away. |
Is that expected behavior? Is there no way around this without enabling the check? |
I'm not 100% sure why the type isn't narrowed without strict null checks. If I had to guess, I would say because The "way around this" is to use |
I mean even if I type it as |
If my understanding is correct, the lack of strict null checks means |
Duplicate of #29862. With type ApiJsonResponse<T> = ApiJsonError | ApiJsonData<T>;
interface ApiJsonData<T> {
success: true | undefined | null;
data: T;
}
interface ApiJsonError {
success: false | undefined | null;
error: string;
} With that in mind, clearly |
That feels weird to me to be honest, it widens "true" or "false" into also undefined and null. |
π Search Terms
jsdoc union type narrow
π Version & Regression Information
v5.8.2
β― Playground Link
https://www.typescriptlang.org/play/?filetype=js#code/PQKhCgAIUgBAXApgWwA4BsCGTIBUowICeqiAJogGaQDeAgqgJYBSAzgPYB2AogE6-tekAD6QGLDpwAi2TAB5cAPgC+Ypmy4AlRK1RdWiAsHDhQEaHCRosOfBeKkK1GgHkARgCtEAY3irxGtKyBHCoAqS88ES08LwAroiqrHHe3jqsIbBh7BFRtLiqZMHQxqZgmVGOVLTuXr7+6pJ8AryZ2bnRNJSY6AZJKWmsGfbtiJGdrLGMnADmqmMtRiaUiPDeABYAFJAA5Jg7kACU4AB08OuInNu8OnqcBpAAvIqQN7r6iCcekpuHR6fnS7bMyWEiIWibIrwTAALjUEi0tw+ckmvGmM0Uf2ekEwnCIqhAwEgUMwT0U4BoUEg1MYlG2AEISSdkql0v9qdTKRzuZBvPp2OhPuh2DNtkyFoIjgBuKk816rOK8Tgy7nKEzcgBumCEAGsnsTZCcSTLVIcpUA
π» Code
Typescript version: https://www.typescriptlang.org/play/?&filetype=ts#code/CYUwxgNghgTiAEAXAngBwQQVQSwFIGcB7AOwCUR9UT8QAeAFQD54BeeLPI4gURhkJjwAPuxwESAESiIoDRgG4AUItCRYCbMUQgYAMyhhMYrlJlz4Ab0Xwb8fAFcwh-PgBcSGPZBLb8YNKh3eiUAX2VVaDh4TW09AyNOEl5+QStfBycKN3h9CBofWx0U93xEGE0Ac1DlXRBEMAALAAp4AHIoVvgASkUAOkQGkGIWuEpqBBZmUapiGl6AKy4mru6+gaGWlv8Zdw5xMgoZmlpS8uIK5hXJxTTbbF0WgEJtqF6M53xV31vfGzBqQgQEC9CCECpbAK9IoCboFX7wOCIewwYhw+Bhay2ABusHgAGtWH5IS9QrCgA
π Actual behavior
π Expected behavior
No error, and for the type to be narrowed correctly.
Additional information about the issue
In my actual code, I have the types (same as in the typescript playground link), in a
d.ts
file. I simply converted them to jsdoc comments for the playground.It does correctly narrow, if I change it to
if( data.success === false )
The text was updated successfully, but these errors were encountered: