Skip to content

Commit bdcc4eb

Browse files
committed
Implementation for weak types
1 parent 9d6dc36 commit bdcc4eb

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

src/compiler/checker.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4785,9 +4785,14 @@ namespace ts {
47854785
let result = Ternary.True;
47864786
let properties = getPropertiesOfObjectType(target);
47874787
let requireOptionalProperties = relation === subtypeRelation && !(source.flags & TypeFlags.ObjectLiteral);
4788+
let foundMatchingProperty = !isWeak(target);
47884789
for (let targetProp of properties) {
47894790
let sourceProp = getPropertyOfType(source, targetProp.name);
47904791

4792+
if (sourceProp) {
4793+
foundMatchingProperty = true;
4794+
}
4795+
47914796
if (sourceProp !== targetProp) {
47924797
if (!sourceProp) {
47934798
if (!(targetProp.flags & SymbolFlags.Optional) || requireOptionalProperties) {
@@ -4859,6 +4864,14 @@ namespace ts {
48594864
}
48604865
}
48614866
}
4867+
4868+
if (!foundMatchingProperty && getPropertiesOfType(source).length > 0) {
4869+
if (reportErrors) {
4870+
reportError(Diagnostics.Weak_type_0_has_no_properties_in_common_with_1, typeToString(target), typeToString(source));
4871+
}
4872+
return Ternary.False;
4873+
}
4874+
48624875
return result;
48634876
}
48644877

@@ -5121,6 +5134,17 @@ namespace ts {
51215134
return false;
51225135
}
51235136

5137+
// A type is 'weak' if it is an object type with at least one optional property
5138+
// and no required properties or index signatures
5139+
function isWeak(type: Type) {
5140+
let props = getPropertiesOfType(type);
5141+
return type.flags & TypeFlags.ObjectType &&
5142+
props.length > 0 &&
5143+
!forEach(props, p => !(p.flags & SymbolFlags.Optional)) &&
5144+
!getIndexTypeOfType(type, IndexKind.String) &&
5145+
!getIndexTypeOfType(type, IndexKind.Number);
5146+
}
5147+
51245148
function isPropertyIdenticalTo(sourceProp: Symbol, targetProp: Symbol): boolean {
51255149
return compareProperties(sourceProp, targetProp, compareTypes) !== Ternary.False;
51265150
}

src/compiler/diagnosticInformationMap.generated.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ namespace ts {
414414
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." },
415415
yield_expressions_cannot_be_used_in_a_parameter_initializer: { code: 2523, category: DiagnosticCategory.Error, key: "'yield' expressions cannot be used in a parameter initializer." },
416416
await_expressions_cannot_be_used_in_a_parameter_initializer: { code: 2524, category: DiagnosticCategory.Error, key: "'await' expressions cannot be used in a parameter initializer." },
417+
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}'." },
417418
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." },
418419
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." },
419420
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." },

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1645,6 +1645,10 @@
16451645
"category": "Error",
16461646
"code": 2524
16471647
},
1648+
"Weak type '{0}' has no properties in common with '{1}'.": {
1649+
"category": "Error",
1650+
"code": 2525
1651+
},
16481652
"JSX element attributes type '{0}' must be an object type.": {
16491653
"category": "Error",
16501654
"code": 2600

src/server/editorServices.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ namespace ts.server {
912912
var dirPath = ts.getDirectoryPath(configFilename);
913913
var rawConfig: { config?: ProjectOptions; error?: Diagnostic; } = ts.readConfigFile(configFilename);
914914
if (rawConfig.error) {
915-
return rawConfig.error;
915+
return { errorMsg: ts.flattenDiagnosticMessageText(rawConfig.error.messageText, '\n') };
916916
}
917917
else {
918918
var parsedCommandLine = ts.parseConfigFile(rawConfig.config, this.host, dirPath);

0 commit comments

Comments
 (0)