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

Wrong unknown inference after String() in 4.1.0-dev.20200819 #40132

Closed
ulrichb opened this issue Aug 19, 2020 · 6 comments
Closed

Wrong unknown inference after String() in 4.1.0-dev.20200819 #40132

ulrichb opened this issue Aug 19, 2020 · 6 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@ulrichb
Copy link

ulrichb commented Aug 19, 2020

TypeScript Version: 4.1.0-dev.20200819

Search Terms: unknown inference string

Code

declare const thrownError: { code?: unknown; }

const thrownErrorCode = thrownError?.code && String(thrownError.code);

const str: string | undefined = thrownErrorCode;

Expected behavior: No Error

Actual behavior:
Error: Type 'unknown' is not assignable to type 'string'.(2322) in the last line.

Playground Link:

https://www.typescriptlang.org/play?ts=4.1.0-dev.20200819#code/CYUwxgNghgTiAEYD2A7AzgF3hgFjJA7igKIz4wBc8A3okqAPxUCuKA1ioSgNzwC+AWABQw5Oiy58RUuQDC9BAF5seLjKQwGAOmSh4AMn3wAyhhgBLFAHMAFJLVkNOhQEpuw0akzxMlH2csreAAfeFZQADNLEGB4ZXtpRxh5UHchIA

Working version with 4.1.0-dev.20200818:

https://www.typescriptlang.org/play?ts=4.1.0-dev.20200818#code/CYUwxgNghgTiAEYD2A7AzgF3hgFjJA7igKIz4wBc8A3okqAPxUCuKA1ioSgNzwC+AWABQw5Oiy58RUuQDC9BAF5seLjKQwGAOmSh4AMn3wAyhhgBLFAHMAFJLVkNOhQEpuw0akzxMlH2csreAAfeFZQADNLEGB4ZXtpRxh5UHchIA

EDIT: Added missing | undefined

@AlCalzone
Copy link
Contributor

@DanielRosenwasser
Copy link
Member

This is a known break coming up in 4.1, but pinging @andrewbranch just because we want to watch the issue tracker to gauge impact here.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 19, 2020
@ulrichb
Copy link
Author

ulrichb commented Aug 19, 2020

@AlCalzone and @DanielRosenwasser: Okay forgot the | undefined ... but the argument stays the same => wrong inferred unknown => updated my repro sample.

@ulrichb
Copy link
Author

ulrichb commented Aug 19, 2020

@RyanCavanaugh duplicate of what? #39113 is the causing issue, not a duplicate ...

@DanielRosenwasser DanielRosenwasser added Working as Intended The behavior described is the intended behavior; this is not a bug and removed Duplicate An existing issue was already created labels Aug 19, 2020
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Aug 19, 2020

But change code to 0 and you'll have the same issue, right?

type ThrownError = { code: unknown; }

const thrownError: ThrownError = {code: 0};
const thrownErrorCode = thrownError?.code && String(thrownError.code);
const str: string = thrownErrorCode;

str.toLocaleLowerCase();

@andrewbranch
Copy link
Member

andrewbranch commented Aug 19, 2020

Yep, the actual domain of of str is string | false | 0 | 0n | NaN (if NaN were a type in TypeScript), so we leave that as unknown instead of giving you a huge union.

@ulrichb ulrichb closed this as completed Aug 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants