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

Make the relationship between partial mapped types and the empty object not apply for subtype relationship #29384

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12567,7 +12567,7 @@ namespace ts {
}
else {
// An empty object type is related to any mapped type that includes a '?' modifier.
if (isPartialMappedType(target) && isEmptyObjectType(source)) {
if (relation !== subtypeRelation && isPartialMappedType(target) && isEmptyObjectType(source)) {
return Ternary.True;
}
if (isGenericMappedType(target)) {
Expand Down
42 changes: 42 additions & 0 deletions tests/baselines/reference/partialTypeNarrowedToByTypeGuard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
//// [partialTypeNarrowedToByTypeGuard.ts]
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 '';
}

//// [partialTypeNarrowedToByTypeGuard.js]
"use strict";
// type PartialUser = {
// email?: string;
// name?: string;
// };
function isUser(obj) {
return true;
}
function getUserName(obj) {
if (isUser(obj)) {
return obj.name;
}
return '';
}
52 changes: 52 additions & 0 deletions tests/baselines/reference/partialTypeNarrowedToByTypeGuard.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
=== tests/cases/compiler/partialTypeNarrowedToByTypeGuard.ts ===
type Obj = {} | undefined;
>Obj : Symbol(Obj, Decl(partialTypeNarrowedToByTypeGuard.ts, 0, 0))

type User = {
>User : Symbol(User, Decl(partialTypeNarrowedToByTypeGuard.ts, 0, 26))

email: string;
>email : Symbol(email, Decl(partialTypeNarrowedToByTypeGuard.ts, 2, 13))

name: string;
>name : Symbol(name, Decl(partialTypeNarrowedToByTypeGuard.ts, 3, 18))

};

type PartialUser = Partial<User>;
>PartialUser : Symbol(PartialUser, Decl(partialTypeNarrowedToByTypeGuard.ts, 5, 2))
>Partial : Symbol(Partial, Decl(lib.es5.d.ts, --, --))
>User : Symbol(User, Decl(partialTypeNarrowedToByTypeGuard.ts, 0, 26))

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

function isUser(obj: Obj): obj is PartialUser {
>isUser : Symbol(isUser, Decl(partialTypeNarrowedToByTypeGuard.ts, 7, 33))
>obj : Symbol(obj, Decl(partialTypeNarrowedToByTypeGuard.ts, 14, 16))
>Obj : Symbol(Obj, Decl(partialTypeNarrowedToByTypeGuard.ts, 0, 0))
>obj : Symbol(obj, Decl(partialTypeNarrowedToByTypeGuard.ts, 14, 16))
>PartialUser : Symbol(PartialUser, Decl(partialTypeNarrowedToByTypeGuard.ts, 5, 2))

return true;
}

function getUserName(obj: Obj) {
>getUserName : Symbol(getUserName, Decl(partialTypeNarrowedToByTypeGuard.ts, 16, 1))
>obj : Symbol(obj, Decl(partialTypeNarrowedToByTypeGuard.ts, 18, 21))
>Obj : Symbol(Obj, Decl(partialTypeNarrowedToByTypeGuard.ts, 0, 0))

if (isUser(obj)) {
>isUser : Symbol(isUser, Decl(partialTypeNarrowedToByTypeGuard.ts, 7, 33))
>obj : Symbol(obj, Decl(partialTypeNarrowedToByTypeGuard.ts, 18, 21))

return obj.name;
>obj.name : Symbol(name, Decl(partialTypeNarrowedToByTypeGuard.ts, 3, 18))
>obj : Symbol(obj, Decl(partialTypeNarrowedToByTypeGuard.ts, 18, 21))
>name : Symbol(name, Decl(partialTypeNarrowedToByTypeGuard.ts, 3, 18))
}

return '';
}
49 changes: 49 additions & 0 deletions tests/baselines/reference/partialTypeNarrowedToByTypeGuard.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
=== tests/cases/compiler/partialTypeNarrowedToByTypeGuard.ts ===
type Obj = {} | undefined;
>Obj : Obj

type User = {
>User : User

email: string;
>email : string

name: string;
>name : string

};

type PartialUser = Partial<User>;
>PartialUser : Partial<User>

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

function isUser(obj: Obj): obj is PartialUser {
>isUser : (obj: Obj) => obj is Partial<User>
>obj : Obj

return true;
>true : true
}

function getUserName(obj: Obj) {
>getUserName : (obj: Obj) => string | undefined
>obj : Obj

if (isUser(obj)) {
>isUser(obj) : boolean
>isUser : (obj: Obj) => obj is Partial<User>
>obj : Obj

return obj.name;
>obj.name : string | undefined
>obj : Partial<User>
>name : string | undefined
}

return '';
>'' : ""
}
26 changes: 26 additions & 0 deletions tests/cases/compiler/partialTypeNarrowedToByTypeGuard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// @strict: true
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 '';
}