Skip to content

Property type is not narrowed for readonly object in some contexts #19469

Description

TypeScript Version: 2.4.1 and 2.6.0-rc

Code

{typeUser={name: string};constlogUser=(_user: User)=>{};typeState={user: User|undefined;};constfn=function<Ownextends{}>(p: Readonly<Own&State>){const{ user }=p;if(user!==undefined){// Unexpected error: Type 'undefined' is not assignable to type 'User'// `user` should be narrowed to exclude `undefined` here.logUser(user);}};}//// KNOWN WORKAROUNDS//{typeUser={name: string};constlogUser=(_user: User)=>{};typeState={user: User|undefined;};constfn=function<Ownextends{}>(p: Own&State){const{ user }=p;if(user!==undefined){// No error when `Readonly` is droppedlogUser(user);}};}{typeUser={name: string};constlogUser=(_user: User)=>{};typeState={user: User|undefined;};constfn=function<Ownextends{}>(p: Readonly<Own&State>){constuser: User|undefined=p.user;if(user!==undefined){// No error when `user` is manually annotatedlogUser(user);}};}{typeUser={name: string};constlogUser=(_user: User)=>{};typeState={user: User|undefined;};typeOwn={};typeP=Readonly<Own&State>constp={}asanyasP;const{ user }=p;if(user!==undefined){// No error if origin of `p` value and `Own` type is not from functionlogUser(user);}}

Expected behavior:
(See comments in code.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions