Skip to content

strictNullChecks blocks safe typeof capture #16108

Closed
@dylanscott

Description

@dylanscott

Sorry if this is a dupe - I tried searching but github's search isn't great.

TypeScript Version: 2.3.1

Code

interface Nested {
    child: {
        prop: string
    }
}

declare function maybeGetNestedValue(): Nested | null

const nested = maybeGetNestedValue()

const child: typeof nested.child = {
    prop: 'value'
}

TypeScript Playground link
Note: You need to manually enable strictNullChecks under the options for this link. If I enable it and try to share it says the text buffer is too big to share.

Expected behavior: I would expect to be able to use typeof to capture the type of this nested field, since as you can see in the emitted code in the TypeScript Playground link, there is no dereference of the nested field emitted, so the code should always be safe.

Actual behavior: The strictNullChecks flag raises an error because the nested field may be null.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions