Skip to content

Print better errors for unusual void, null and undefined types #1303

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

Merged
merged 3 commits into from
May 28, 2020

Conversation

dcodeIO
Copy link
Member

@dcodeIO dcodeIO commented May 28, 2020

Fixes #558 by making

export function test1(x: void): void {}

export function test2(x: undefined): void {}

export function test3(x: null): void {}

export function test4(a: string | undefined): void {}

now diagnosing

ERROR TS1110: Type expected.

export function test1(x: void): void {}
                         ~~~~
in input.ts(1,26)
ERROR TS2304: Cannot find name 'undefined'.

export function test2(x: undefined): void {}
                         ~~~~~~~~~
in input.ts(3,26)
ERROR TS2304: Cannot find name 'null'.

export function test3(x: null): void {}
                         ~~~~
in input.ts(5,26)
ERROR TS1005: 'null' expected.

export function test4(a: string | undefined): void {}
                                  ~~~~~~~~~
in input.ts(7,35)

with test4 diagnosed early on parser level.

@dcodeIO dcodeIO requested a review from MaxGraey May 28, 2020 11:03
@MaxGraey
Copy link
Member

MaxGraey commented May 28, 2020

Could it also handle this cases?

let x: void;
let y = <void>1;
let z: undefined;
let w = <undefined>1;

export function test5(a: string): void | undefined {}
                                         ~~~~~~~~~

export function test6(a: string): void | null {}
                                  ~~~~~~~~~~~

Copy link
Member

@MaxGraey MaxGraey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM

@dcodeIO
Copy link
Member Author

dcodeIO commented May 28, 2020

The new given examples diagnose

let x: void; // TS1110: Type expected.
let y = <void>1; // TS2322: Type 'void' is not assignable to type '<auto>'.
let z: undefined; // TS2304: Cannot find name 'undefined'.
let w = <undefined>1; // TS2304: Cannot find name 'undefined'.

while a void | something return type seems to exit early on void, leading to TS2391: Function implementation is missing or not immediately following the declaration kind of errors. Going to see if I can do something about these as well.

@dcodeIO
Copy link
Member Author

dcodeIO commented May 28, 2020

Last commit changes the parsing of union return types to always occur, so

export function test6(a: string): void | null {}

now diagnoses

ERROR AS204: Basic type 'void' cannot be nullable.

export function test6(a: string): void | null {}
                                  ~~~~
in input.ts(1,35)

and

export function test5(a: string): void | undefined {}

yields

ERROR TS1005: 'null' expected.

export function test5(a: string): void | undefined {}
                                         ~~~~~~~~~
in input.ts(1,42)

@MaxGraey
Copy link
Member

Great! Well done!

@dcodeIO dcodeIO merged commit 451dd4d into master May 28, 2020
@dcodeIO dcodeIO deleted the void-null-undefined-errors branch June 11, 2020 17:17
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

Successfully merging this pull request may close these issues.

Print better errors for unusual using void, null and undefined types
2 participants