Skip to content

strictNullChecks limits what types can be accessed with indexed access types #21975

Closed
@nomcopter

Description

@nomcopter

TypeScript Version: 2.7.1-insiders.20180127

Search Terms: strictNullChecks mapped types property undefined generated types

Code

interface Foo {
    bar?: {
        baz: string;
    }
}

type ShouldBeString = Foo['bar']['baz'];

Expected behavior: Type ShouldBeString is a string type. This is useful especially for generated types where it isn't possible to write { baz: string } as a separate type.

Actual behavior: Accessing 'baz' errors with Property 'baz' does not exist on type '{ baz: string; } | undefined'.

Playground Link:
Make sure to toggle strictNullChecks on: https://www.typescriptlang.org/play/#src=interface%20Foo%20%7B%0D%0A%20%20%20%20bar%3F%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20baz%3A%20string%3B%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Atype%20ShouldBeString%20%3D%20Foo%5B'bar'%5D%5B'baz'%5D

Related Issues: #16108 #6606

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions