Skip to content
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
11 changes: 4 additions & 7 deletions internal/checker/relater.go
Original file line number Diff line number Diff line change
Expand Up @@ -4153,16 +4153,13 @@ func (r *Relater) propertiesRelatedTo(source *Type, target *Type, reportErrors b
}
return TernaryFalse
}
if isObjectLiteralType(target) {
if target.objectFlags&ObjectFlagsFreshLiteral != 0 {
for _, sourceProp := range excludeProperties(r.c.getPropertiesOfType(source), excludedProperties) {
if r.c.getPropertyOfObjectType(target, sourceProp.Name) == nil {
sourceType := r.c.getTypeOfSymbol(sourceProp)
if sourceType.flags&TypeFlagsUndefined == 0 {
if reportErrors {
r.reportError(diagnostics.Property_0_does_not_exist_on_type_1, r.c.symbolToString(sourceProp), r.c.TypeToString(target))
}
return TernaryFalse
if reportErrors {
r.reportError(diagnostics.Property_0_does_not_exist_on_type_1, r.c.symbolToString(sourceProp), r.c.TypeToString(target))
}
return TernaryFalse
}
}
}
Comment on lines +4156 to 4165
Copy link

Copilot AI Sep 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The excess property checking logic has been simplified but the removal of the undefined type check may be too broad. The previous logic if sourceType.flags&TypeFlagsUndefined == 0 ensured that undefined properties weren't considered excess properties, which might be important for optional property handling. Consider whether this check should be preserved to maintain correct behavior for optional properties.

Copilot uses AI. Check for mistakes.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intended.

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
//// [tests/cases/compiler/freshObjectLiteralSubtype.ts] ////

=== freshObjectLiteralSubtype.ts ===
function f1() {
>f1 : Symbol(f1, Decl(freshObjectLiteralSubtype.ts, 0, 0))

if (!!true) {
return { valid: true }
>valid : Symbol(valid, Decl(freshObjectLiteralSubtype.ts, 2, 16))
}
return f2()
>f2 : Symbol(f2, Decl(freshObjectLiteralSubtype.ts, 7, 13))
}

declare const f2: () => { valid: boolean, msg?: undefined }
>f2 : Symbol(f2, Decl(freshObjectLiteralSubtype.ts, 7, 13))
>valid : Symbol(valid, Decl(freshObjectLiteralSubtype.ts, 7, 25))
>msg : Symbol(msg, Decl(freshObjectLiteralSubtype.ts, 7, 41))

f1().msg
>f1().msg : Symbol(msg, Decl(freshObjectLiteralSubtype.ts, 7, 41))
>f1 : Symbol(f1, Decl(freshObjectLiteralSubtype.ts, 0, 0))
>msg : Symbol(msg, Decl(freshObjectLiteralSubtype.ts, 7, 41))

// Repro from https://github.com/microsoft/typescript-go/issues/1742

function validate() {
>validate : Symbol(validate, Decl(freshObjectLiteralSubtype.ts, 9, 8))

if(Math.random() > 0.5) {
>Math.random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))
>Math : Symbol(Math, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))
>random : Symbol(Math.random, Decl(lib.es5.d.ts, --, --))

return utilValidate();
>utilValidate : Symbol(utilValidate, Decl(freshObjectLiteralSubtype.ts, 18, 2))
}
return { valid: true };
>valid : Symbol(valid, Decl(freshObjectLiteralSubtype.ts, 17, 12))

};


declare function utilValidate(): {
>utilValidate : Symbol(utilValidate, Decl(freshObjectLiteralSubtype.ts, 18, 2))

valid: boolean;
>valid : Symbol(valid, Decl(freshObjectLiteralSubtype.ts, 21, 34))

msg?: undefined;
>msg : Symbol(msg, Decl(freshObjectLiteralSubtype.ts, 22, 19))

} | {
valid: boolean;
>valid : Symbol(valid, Decl(freshObjectLiteralSubtype.ts, 24, 5))

msg: string;
>msg : Symbol(msg, Decl(freshObjectLiteralSubtype.ts, 25, 19))
}

validate().msg; // Error in TSGO
>validate().msg : Symbol(msg, Decl(freshObjectLiteralSubtype.ts, 22, 19), Decl(freshObjectLiteralSubtype.ts, 25, 19))
>validate : Symbol(validate, Decl(freshObjectLiteralSubtype.ts, 9, 8))
>msg : Symbol(msg, Decl(freshObjectLiteralSubtype.ts, 22, 19), Decl(freshObjectLiteralSubtype.ts, 25, 19))

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
//// [tests/cases/compiler/freshObjectLiteralSubtype.ts] ////

=== freshObjectLiteralSubtype.ts ===
function f1() {
>f1 : () => { valid: boolean; msg?: undefined; }

if (!!true) {
>!!true : true
>!true : false
>true : true

return { valid: true }
>{ valid: true } : { valid: boolean; }
>valid : boolean
>true : true
}
return f2()
>f2() : { valid: boolean; msg?: undefined; }
>f2 : () => { valid: boolean; msg?: undefined; }
}

declare const f2: () => { valid: boolean, msg?: undefined }
>f2 : () => { valid: boolean; msg?: undefined; }
>valid : boolean
>msg : undefined

f1().msg
>f1().msg : undefined
>f1() : { valid: boolean; msg?: undefined; }
>f1 : () => { valid: boolean; msg?: undefined; }
>msg : undefined

// Repro from https://github.com/microsoft/typescript-go/issues/1742

function validate() {
>validate : () => { valid: boolean; msg?: undefined; } | { valid: boolean; msg: string; }

if(Math.random() > 0.5) {
>Math.random() > 0.5 : boolean
>Math.random() : number
>Math.random : () => number
>Math : Math
>random : () => number
>0.5 : 0.5

return utilValidate();
>utilValidate() : { valid: boolean; msg?: undefined; } | { valid: boolean; msg: string; }
>utilValidate : () => { valid: boolean; msg?: undefined; } | { valid: boolean; msg: string; }
}
return { valid: true };
>{ valid: true } : { valid: boolean; }
>valid : boolean
>true : true

};


declare function utilValidate(): {
>utilValidate : () => { valid: boolean; msg?: undefined; } | { valid: boolean; msg: string; }

valid: boolean;
>valid : boolean

msg?: undefined;
>msg : undefined

} | {
valid: boolean;
>valid : boolean

msg: string;
>msg : string
}

validate().msg; // Error in TSGO
>validate().msg : string | undefined
>validate() : { valid: boolean; msg?: undefined; } | { valid: boolean; msg: string; }
>validate : () => { valid: boolean; msg?: undefined; } | { valid: boolean; msg: string; }
>msg : string | undefined

33 changes: 33 additions & 0 deletions testdata/tests/cases/compiler/freshObjectLiteralSubtype.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// @strict: true
// @noEmit: true

function f1() {
if (!!true) {
return { valid: true }
}
return f2()
}

declare const f2: () => { valid: boolean, msg?: undefined }

f1().msg

// Repro from https://github.com/microsoft/typescript-go/issues/1742

function validate() {
if(Math.random() > 0.5) {
return utilValidate();
}
return { valid: true };
};


declare function utilValidate(): {
valid: boolean;
msg?: undefined;
} | {
valid: boolean;
msg: string;
}

validate().msg; // Error in TSGO
Loading