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

Weak type detection #3842

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 24 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4785,9 +4785,14 @@ namespace ts {
let result = Ternary.True;
let properties = getPropertiesOfObjectType(target);
let requireOptionalProperties = relation === subtypeRelation && !(source.flags & TypeFlags.ObjectLiteral);
let foundMatchingProperty = !isWeak(target);
for (let targetProp of properties) {
let sourceProp = getPropertyOfType(source, targetProp.name);

if (sourceProp) {
foundMatchingProperty = true;
}

if (sourceProp !== targetProp) {
if (!sourceProp) {
if (!(targetProp.flags & SymbolFlags.Optional) || requireOptionalProperties) {
Expand Down Expand Up @@ -4859,6 +4864,14 @@ namespace ts {
}
}
}

if (!foundMatchingProperty && getPropertiesOfType(source).length > 0) {
if (reportErrors) {
reportError(Diagnostics.Weak_type_0_has_no_properties_in_common_with_1, typeToString(target), typeToString(source));
}
return Ternary.False;
}

return result;
}

Expand Down Expand Up @@ -5121,6 +5134,17 @@ namespace ts {
return false;
}

// A type is 'weak' if it is an object type with at least one optional property
// and no required properties or index signatures
function isWeak(type: Type) {
let props = getPropertiesOfType(type);
return type.flags & TypeFlags.ObjectType &&
props.length > 0 &&
!forEach(props, p => !(p.flags & SymbolFlags.Optional)) &&
!getIndexTypeOfType(type, IndexKind.String) &&
!getIndexTypeOfType(type, IndexKind.Number);
}

function isPropertyIdenticalTo(sourceProp: Symbol, targetProp: Symbol): boolean {
return compareProperties(sourceProp, targetProp, compareTypes) !== Ternary.False;
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/diagnosticInformationMap.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ namespace ts {
The_arguments_object_cannot_be_referenced_in_an_async_arrow_function_Consider_using_a_standard_async_function_expression: { code: 2522, category: DiagnosticCategory.Error, key: "The 'arguments' object cannot be referenced in an async arrow function. Consider using a standard async function expression." },
yield_expressions_cannot_be_used_in_a_parameter_initializer: { code: 2523, category: DiagnosticCategory.Error, key: "'yield' expressions cannot be used in a parameter initializer." },
await_expressions_cannot_be_used_in_a_parameter_initializer: { code: 2524, category: DiagnosticCategory.Error, key: "'await' expressions cannot be used in a parameter initializer." },
Weak_type_0_has_no_properties_in_common_with_1: { code: 2525, category: DiagnosticCategory.Error, key: "Weak type '{0}' has no properties in common with '{1}'." },
JSX_element_attributes_type_0_must_be_an_object_type: { code: 2600, category: DiagnosticCategory.Error, key: "JSX element attributes type '{0}' must be an object type." },
The_return_type_of_a_JSX_element_constructor_must_return_an_object_type: { code: 2601, category: DiagnosticCategory.Error, key: "The return type of a JSX element constructor must return an object type." },
JSX_element_implicitly_has_type_any_because_the_global_type_JSX_Element_does_not_exist: { code: 2602, category: DiagnosticCategory.Error, key: "JSX element implicitly has type 'any' because the global type 'JSX.Element' does not exist." },
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -1645,6 +1645,10 @@
"category": "Error",
"code": 2524
},
"Weak type '{0}' has no properties in common with '{1}'.": {
"category": "Error",
"code": 2525
},
"JSX element attributes type '{0}' must be an object type.": {
"category": "Error",
"code": 2600
Expand Down
2 changes: 1 addition & 1 deletion src/server/editorServices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -912,7 +912,7 @@ namespace ts.server {
var dirPath = ts.getDirectoryPath(configFilename);
var rawConfig: { config?: ProjectOptions; error?: Diagnostic; } = ts.readConfigFile(configFilename);
if (rawConfig.error) {
return rawConfig.error;
return { errorMsg: ts.flattenDiagnosticMessageText(rawConfig.error.messageText, '\n') };
}
else {
var parsedCommandLine = ts.parseConfigFile(rawConfig.config, this.host, dirPath);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts(33,5): error TS2322: Type 'D' is not assignable to type 'C'.
Weak type 'C' has no properties in common with 'D'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts(34,5): error TS2322: Type 'E' is not assignable to type 'C'.
Weak type 'C' has no properties in common with 'E'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts(35,5): error TS2322: Type 'F' is not assignable to type 'C'.
Weak type 'C' has no properties in common with 'F'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts(38,5): error TS2322: Type 'D' is not assignable to type '{ opt?: Base; }'.
Weak type '{ opt?: Base; }' has no properties in common with 'D'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts(39,5): error TS2322: Type 'E' is not assignable to type '{ opt?: Base; }'.
Weak type '{ opt?: Base; }' has no properties in common with 'E'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts(40,5): error TS2322: Type 'F' is not assignable to type '{ opt?: Base; }'.
Weak type '{ opt?: Base; }' has no properties in common with 'F'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts(43,5): error TS2322: Type 'D' is not assignable to type '{ opt?: Base; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts(44,5): error TS2322: Type 'E' is not assignable to type '{ opt?: Base; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts(45,5): error TS2322: Type 'F' is not assignable to type '{ opt?: Base; }'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts(74,5): error TS2322: Type 'D' is not assignable to type 'C'.
Property 'opt' is missing in type 'D'.
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts(75,5): error TS2322: Type 'E' is not assignable to type 'C'.
Expand All @@ -18,7 +33,7 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme
Property 'opt' is missing in type 'F'.


==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts (9 errors) ====
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignmentCompatWithObjectMembersOptionality2.ts (18 errors) ====
// M is optional and S contains no property with the same name as M
// N is optional and T contains no property with the same name as N

Expand Down Expand Up @@ -52,18 +67,42 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/assignme

// all ok
c = d;
~
!!! error TS2322: Type 'D' is not assignable to type 'C'.
!!! error TS2322: Weak type 'C' has no properties in common with 'D'.
c = e;
~
!!! error TS2322: Type 'E' is not assignable to type 'C'.
!!! error TS2322: Weak type 'C' has no properties in common with 'E'.
c = f;
~
!!! error TS2322: Type 'F' is not assignable to type 'C'.
!!! error TS2322: Weak type 'C' has no properties in common with 'F'.
c = a;

a = d;
~
!!! error TS2322: Type 'D' is not assignable to type '{ opt?: Base; }'.
!!! error TS2322: Weak type '{ opt?: Base; }' has no properties in common with 'D'.
a = e;
~
!!! error TS2322: Type 'E' is not assignable to type '{ opt?: Base; }'.
!!! error TS2322: Weak type '{ opt?: Base; }' has no properties in common with 'E'.
a = f;
~
!!! error TS2322: Type 'F' is not assignable to type '{ opt?: Base; }'.
!!! error TS2322: Weak type '{ opt?: Base; }' has no properties in common with 'F'.
a = c;

b = d;
~
!!! error TS2322: Type 'D' is not assignable to type '{ opt?: Base; }'.
b = e;
~
!!! error TS2322: Type 'E' is not assignable to type '{ opt?: Base; }'.
b = f;
~
!!! error TS2322: Type 'F' is not assignable to type '{ opt?: Base; }'.
b = a;
b = c;
}
Expand Down
17 changes: 16 additions & 1 deletion tests/baselines/reference/subtypingWithObjectMembers5.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW
Property '1' is missing in type 'B2'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithObjectMembers5.ts(32,11): error TS2420: Class 'B3' incorrectly implements interface 'A3'.
Property ''1'' is missing in type 'B3'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithObjectMembers5.ts(43,11): error TS2420: Class 'B' incorrectly implements interface 'A'.
Weak type 'A' has no properties in common with 'B'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithObjectMembers5.ts(51,11): error TS2420: Class 'B2' incorrectly implements interface 'A2'.
Weak type 'A2' has no properties in common with 'B2'.
tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithObjectMembers5.ts(59,11): error TS2420: Class 'B3' incorrectly implements interface 'A3'.
Weak type 'A3' has no properties in common with 'B3'.


==== tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithObjectMembers5.ts (3 errors) ====
==== tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingWithObjectMembers5.ts (6 errors) ====
interface Base {
foo: string;
}
Expand Down Expand Up @@ -59,6 +65,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW
}

class B implements A {
~
!!! error TS2420: Class 'B' incorrectly implements interface 'A'.
!!! error TS2420: Weak type 'A' has no properties in common with 'B'.
fooo: Derived; // ok
}

Expand All @@ -67,6 +76,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW
}

class B2 implements A2 {
~~
!!! error TS2420: Class 'B2' incorrectly implements interface 'A2'.
!!! error TS2420: Weak type 'A2' has no properties in common with 'B2'.
2: Derived; // ok
}

Expand All @@ -75,6 +87,9 @@ tests/cases/conformance/types/typeRelationships/subtypesAndSuperTypes/subtypingW
}

class B3 implements A3 {
~~
!!! error TS2420: Class 'B3' incorrectly implements interface 'A3'.
!!! error TS2420: Weak type 'A3' has no properties in common with 'B3'.
'1.0': Derived; // ok
}
}
Loading