Skip to content

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

@shilangyu

Description

@shilangyu

TypeScript Version: 3.7.2

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

typeSomeType=string|undefinedclassTest{prop: SomeTypefunc1=(): this['prop']=>{returnthis.prop}func2=(): SomeType=>{returnthis.prop}func3=(): NonNullable<this['prop']>=>{returnthis.prop!}func4=(): NonNullable<SomeType>=>{returnthis.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