From 73cb835c8c125b7c44fb351f3040d3e6a9ddf0c1 Mon Sep 17 00:00:00 2001 From: Nathan Shively-Sanders <293473+sandersn@users.noreply.github.com> Date: Tue, 29 May 2018 11:00:38 -0700 Subject: [PATCH 1/3] Skip IntrinsicAttributes elaboration in JSX errors Do not issue an error message for a source type that comes from JSX attributes and a target type that is an intersection containing IntrinsicAttributes or IntrinsicClassAttributes. This will make error messages simpler and less confusing. Note: 1. There will always be elaboration under the skipped message, so this won't elide errors completely. 2. Rarely (once in the tests) the intersection type will have more that one non-Intrinsic* member. However, these additional members don't provide useful information either, so it's fine to skip them. --- src/compiler/checker.ts | 14 ++- .../checkJsxChildrenProperty14.errors.txt | 18 ++-- .../checkJsxChildrenProperty2.errors.txt | 98 +++++++++---------- .../checkJsxChildrenProperty4.errors.txt | 18 ++-- .../checkJsxChildrenProperty5.errors.txt | 46 ++++----- .../checkJsxChildrenProperty7.errors.txt | 62 ++++++------ ...StringLiteralsInJsxAttributes02.errors.txt | 20 ++-- ...ponentWithDefaultTypeParameter3.errors.txt | 10 +- ...tsxSpreadAttributesResolution12.errors.txt | 14 ++- ...tsxSpreadAttributesResolution16.errors.txt | 10 +- .../tsxSpreadAttributesResolution2.errors.txt | 48 ++++----- .../tsxSpreadAttributesResolution5.errors.txt | 14 ++- .../tsxSpreadAttributesResolution6.errors.txt | 10 +- ...elessFunctionComponentOverload4.errors.txt | 44 ++++----- ...elessFunctionComponentOverload5.errors.txt | 50 ++++------ ...tsxStatelessFunctionComponents1.errors.txt | 20 ++-- ...ionComponentsWithTypeArguments4.errors.txt | 10 +- .../reference/tsxUnionElementType6.errors.txt | 20 ++-- 18 files changed, 230 insertions(+), 296 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 237553141f666..57097377dc6c4 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -10592,6 +10592,16 @@ namespace ts { else if (source.symbol && source.flags & TypeFlags.Object && globalObjectType === source) { reportError(Diagnostics.The_Object_type_is_assignable_to_very_few_other_types_Did_you_mean_to_use_the_any_type_instead); } + else if (getObjectFlags(source) & ObjectFlags.JsxAttributes && target.flags & TypeFlags.Intersection) { + const targetTypes = (target as IntersectionType).types; + const intrinsicAttributes = getJsxType(JsxNames.IntrinsicAttributes, errorNode); + const intrinsicClassAttributes = getJsxType(JsxNames.IntrinsicClassAttributes, errorNode); + if (intrinsicAttributes !== unknownType && intrinsicClassAttributes !== unknownType && + (contains(targetTypes, intrinsicAttributes) || contains(targetTypes, intrinsicClassAttributes))) { + // do not report top error + return result; + } + } reportRelationError(headMessage, source, target); } return result; @@ -16274,7 +16284,7 @@ namespace ts { return createJsxAttributesTypeFromAttributesProperty(node.parent, checkMode); } - function getJsxType(name: __String, location: Node) { + function getJsxType(name: __String, location: Node | undefined) { const namespace = getJsxNamespaceAt(location); const exports = namespace && getExportsOfSymbol(namespace); const typeSymbol = exports && getSymbol(exports, name, SymbolFlags.Type); @@ -16372,7 +16382,7 @@ namespace ts { return getSignatureInstantiation(signature, args, isJavascript); } - function getJsxNamespaceAt(location: Node): Symbol { + function getJsxNamespaceAt(location: Node | undefined): Symbol { const namespaceName = getJsxNamespace(location); const resolvedNamespace = resolveName(location, namespaceName, SymbolFlags.Namespace, /*diagnosticMessage*/ undefined, namespaceName, /*isUse*/ false); if (resolvedNamespace) { diff --git a/tests/baselines/reference/checkJsxChildrenProperty14.errors.txt b/tests/baselines/reference/checkJsxChildrenProperty14.errors.txt index 6878372be8b4c..cf999188ebc59 100644 --- a/tests/baselines/reference/checkJsxChildrenProperty14.errors.txt +++ b/tests/baselines/reference/checkJsxChildrenProperty14.errors.txt @@ -1,8 +1,7 @@ -tests/cases/conformance/jsx/file.tsx(42,11): error TS2322: Type '{ children: Element[]; a: number; b: string; }' is not assignable to type 'IntrinsicAttributes & SingleChildProp'. - Type '{ children: Element[]; a: number; b: string; }' is not assignable to type 'SingleChildProp'. - Types of property 'children' are incompatible. - Type 'Element[]' is not assignable to type 'Element'. - Property 'type' is missing in type 'Element[]'. +tests/cases/conformance/jsx/file.tsx(42,11): error TS2322: Type '{ children: Element[]; a: number; b: string; }' is not assignable to type 'SingleChildProp'. + Types of property 'children' are incompatible. + Type 'Element[]' is not assignable to type 'Element'. + Property 'type' is missing in type 'Element[]'. ==== tests/cases/conformance/jsx/file.tsx (1 errors) ==== @@ -49,8 +48,7 @@ tests/cases/conformance/jsx/file.tsx(42,11): error TS2322: Type '{ children: Ele // Error let k5 = <>