Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type guards not working when using partial mapped types. #28434

Closed
willchapin opened this issue Nov 9, 2018 · 2 comments
Closed

Type guards not working when using partial mapped types. #28434

willchapin opened this issue Nov 9, 2018 · 2 comments
Assignees
Labels
Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis Domain: Mapped Types The issue relates to mapped types

Comments

@willchapin
Copy link

willchapin commented Nov 9, 2018

TypeScript Version: 3.1.3

Search Terms:
Type Guards, Partial

Code

type Obj = {} | undefined;

type User = {
  email: string;
  name: string;
};

type PartialUser = Partial<User>;

// type PartialUser = {
//   email?: string;
//   name?: string;
// };

function isUser(obj: Obj): obj is PartialUser {
  return true;
}

function getUserName(obj: Obj) {
  if (isUser(obj)) {
    return obj.name;
  }

  return '';
}

Expected behavior:
Successful compilation of the above code with tsc --strictNullChecks

Actual behavior:
Compilation fails with error TS2339: Property 'name' does not exist on type '{}'.
Interestingly, replacing type PartialUser = Partial<User>; with

type PartialUser = {
  email?: string;
  name?: string;
};

works as expected. Shouldn't these two definitions of PartialUser be functionally equivalent?

Playground Link:
https://agentcooper.github.io/typescript-play/?noImplicitAny=false&strictFunctionTypes=false&strictPropertyInitialization=false&noImplicitThis=false&noImplicitReturns=false&alwaysStrict=false#code/C4TwDgpgBA8gRgKygXigbwL5QD5QK4B2AJhAGYCWBERA3AFB2iRQCqAzhAE4rp1T9QIAWwCG5ADYAuKG2CdKAc3oCoBEUIjTZ8gkroZ6jcNAAKIzsHIjx7LjzMWr4gDy3OAPkMB6L1CanzS2s3HjQ6HwFhMXEAfi05RXoI-jUNOJkE3STfAwZSQgBjSwB7AihyNjcACmLEaXgEAEppWqQKqAcgmw5uMP5OCGA8TjK5PAh6DDzCkrKFQbcAOXUIGrrYREbefnJSKCqK6tbGrb6BAaGRqFaAOlSJvigpx4vhsoByd8mgA

@weswigham weswigham added Bug A bug in TypeScript Domain: Mapped Types The issue relates to mapped types Domain: Control Flow The issue relates to control flow analysis labels Nov 9, 2018
@jcalz
Copy link
Contributor

jcalz commented Nov 11, 2018

@weswigham weswigham added this to the TypeScript 3.3 milestone Dec 8, 2018
@weswigham weswigham self-assigned this Dec 8, 2018
@willchapin
Copy link
Author

@weswigham, have you had a chance to look into this yet? If not, would it be something I could take a stab at? Would love to dig in here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Domain: Control Flow The issue relates to control flow analysis Domain: Mapped Types The issue relates to mapped types
Projects
None yet
Development

No branches or pull requests

3 participants