Skip to content

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

Description

@ulrichb

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions