Skip to content

Object property in anonymous function in typeguard: Should the typeguard work when the object is const and property is readonly? #9511

Closed
@dingfengquek

Description

@dingfengquek

TypeScript Version: 2.0.0-dev.20160704
Code

const x : { readonly a: string | undefined } = { a: 's' };
if (typeof x.a !== 'undefined') {
    () => x.a.length;
  }

Expected behavior:
x.a.length should be valid, since x.a is in a typeguard, x is constant, and x.a is readonly, and thus value of x.a cannot change even when the function is called at a later time.

Actual behavior:
[ts] Object is possibly 'undefined'.
(property) a: string | undefined

Metadata

Metadata

Assignees

Labels

Working as IntendedThe behavior described is the intended behavior; this is not a bug

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions