镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

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

Closed

Description

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

Activity

  1. mhegazy commented on Feb 15, 2018

    @mhegazy
    Contributor

    Duplicate of #16108, and #17370

  2. nomcopter commented on Feb 15, 2018

    @nomcopter
    Author

    I'm don't think this is a duplicate Mohamed Hegazy (@mhegazy). This involves mapped types instead of the typeof operator.

  3. nomcopter commented on Feb 20, 2018

    @nomcopter
    Author

    This issue may generalize more correctly to a feature request that the compiler should chose the "more useful" type when mapping through union types. For example the following example applies without strictNullChecks:

    interface Foo {
        bar: {
            baz: string;
        } | {
            qux: number;
        }
    }
    
    type ShouldBeString = Foo['bar']['baz']
    

    https://www.typescriptlang.org/play/index.html#src=interface%20Foo%20%7B%0D%0A%20%20%20%20bar%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20baz%3A%20string%3B%0D%0A%20%20%20%20%7D%20%7C%20%7B%0D%0A%20%20%20%20%20%20%20%20qux%3A%20number%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

  4. changed the title [-]strictNullChecks limits what types can be accessed with mapped types[/-] [+]strictNullChecks limits what types can be accessed with indexed access typres[/+] on Feb 20, 2018
  5. changed the title [-]strictNullChecks limits what types can be accessed with indexed access typres[/-] [+]strictNullChecks limits what types can be accessed with indexed access types[/+] on Feb 20, 2018
  6. DanielRosenwasser commented on Feb 20, 2018

    @DanielRosenwasser
    Member

    See #14366 for the closer dupe.

  7. nomcopter commented on Feb 20, 2018

    @nomcopter
    Author

    Would it be better to handle #14366 in the more general case by handling all union types? Should I update this issue?

  8. typescript-bot commented on Mar 7, 2018

    @typescript-bot
    Contributor

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

  9. locked and limited conversation to collaborators on Jul 25, 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

    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