Skip to content

NonNullable behaves differently when passing a property type from this #36061

Closed
@shilangyu

Description

@shilangyu

TypeScript Version: 3.7.2

Search Terms:
NonNullable this this[] this property nonnullable
Code

type SomeType = string | undefined

class Test {
    prop: SomeType

    func1 = (): this['prop'] => {
        return this.prop
    }

    func2 = (): SomeType => {
        return this.prop
    }

    func3 = (): NonNullable<this['prop']> => {
        return this.prop!
    }

    func4 = (): NonNullable<SomeType> => {
        return this.prop!
    }
}

Expected behavior:

This code compiles correctly, this['prop'] and SomeType point to the same type.

Actual behavior:

func3 is not happy with the return statement: Type 'string' is not assignable to type 'NonNullable<this["prop"]>'.

Playground Link: link

Related Issues: none

Metadata

Metadata

Assignees

Labels

Needs InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions