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

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

Closed
nomcopter opened this issue Feb 15, 2018 · 6 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@nomcopter
Copy link

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

@mhegazy
Copy link
Contributor

mhegazy commented Feb 15, 2018

Duplicate of #16108, and #17370

@mhegazy mhegazy added the Duplicate An existing issue was already created label Feb 15, 2018
@nomcopter
Copy link
Author

nomcopter commented Feb 15, 2018

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

@nomcopter
Copy link
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

@DanielRosenwasser DanielRosenwasser changed the title strictNullChecks limits what types can be accessed with mapped types strictNullChecks limits what types can be accessed with indexed access typres Feb 20, 2018
@DanielRosenwasser DanielRosenwasser 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 Feb 20, 2018
@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Feb 20, 2018

See #14366 for the closer dupe.

@nomcopter
Copy link
Author

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

@typescript-bot
Copy link
Collaborator

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

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants