Skip to content

Typescript incorrectly infer the types for Readonly optional properties. #18902

Closed
@dryganets

Description

@dryganets

TypeScript Version: 2.5.3) / 2.6.0-dev.20170930

Code

https://github.com/dryganets/typescript-bug/blob/master/index.ts
I expect this code to compile.
This example is
Expected behavior:
type guard

if (this.props.a) {
}

reduces the type from string|undefined to string.

Actual behavior:
type is still string|undefined after check.

The bug doesn't repro if I use the slightly different syntax:

export class Test extends ComponentBase<Props, {}> {

But we have some places in the app where the former syntax is required.

Activity

changed the title [-]Typescript incorrectly infer the types for Readonly members.[/-] [+]Typescript incorrectly infer the types for Readonly optional properties.[/+] on Oct 3, 2017
mhegazy

mhegazy commented on Oct 3, 2017

@mhegazy
Contributor

This is a duplicate of #15376. this.prop.a is a generic type, P["a"], and there is no way currently to narrow a generic type.

It is worth noting that the compiler does the right thing on property access/ index access/ call thanks to #15576. but the type of the symbol does not change.

added
Design LimitationConstraints of the existing architecture prevent this from being fixed
on Oct 3, 2017
mhegazy

mhegazy commented on Oct 17, 2017

@mhegazy
Contributor

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

locked and limited conversation to collaborators on Jun 14, 2018
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

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @dryganets@mhegazy

        Issue actions

          Typescript incorrectly infer the types for Readonly optional properties. · Issue #18902 · microsoft/TypeScript