Skip to content
New issue

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

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

Already on GitHub? # to your account

handle null does not effect for string #2223

Closed
HerrCai0907 opened this issue Mar 8, 2022 · 2 comments
Closed

handle null does not effect for string #2223

HerrCai0907 opened this issue Mar 8, 2022 · 2 comments
Labels

Comments

@HerrCai0907
Copy link
Member

let a = 1;

function foo(): string | null {
  return a > 10 ? null : "";
}

export class B {
  test(): void {
    let t: string | null = foo();
    if (t != null) {
      let v: string = t;  // ERROR TS2322: Type '~lib/string/String | null' is not assignable to type '~lib/string/String'.
    }
  }
}

export function test(): void {
  let t: string | null = foo();
  if (t != null) {
    let v: string = t;   // ERROR TS2322: Type '~lib/string/String | null' is not assignable to type '~lib/string/String'.
  }
}
@dcodeIO
Copy link
Member

dcodeIO commented Mar 16, 2022

This is expected in 0.19, since != calls an operator overload as a function. Right now the compiler does not attempt to prove what this function does. Using !== fixes the issue for now as it cannot be overloaded. Related to the long standed triple equals discrepancy. Will be tackled by 0.20 by unifying these cases and special-casing comparisons with null.

@github-actions
Copy link

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in one week if no further activity occurs. Thank you for your contributions!

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

No branches or pull requests

2 participants