Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- From 19615ccea05214c00f64e0a15724986376e5aa60 Mon Sep 17 00:00:00 2001
- Date: Fri, 15 Mar 2019 01:21:34 +0000
- Subject: [PATCH] Patch
- ---
- src/compiler/checker.ts | 49 ++++++++++++++++++++++++++++++++++++-----
- src/compiler/types.ts | 4 +++-
- 2 files changed, 47 insertions(+), 6 deletions(-)
- diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts
- index 43bc62ee85..7328d3d2fd 100644
- --- a/src/compiler/checker.ts
- +++ b/src/compiler/checker.ts
- @@ -670,7 +670,7 @@ namespace ts {
- let _jsxNamespace: __String;
- let _jsxFactoryEntity: EntityName | undefined;
- - let unmeasurableMarkerHandler: (() => void) | undefined;
- + let unmeasurableMarkerHandler: ((isUnmeasurable: boolean, id?: number) => void) | undefined;
- const subtypeRelation = createMap<RelationComparisonResult>();
- const assignableRelation = createMap<RelationComparisonResult>();
- @@ -12612,6 +12612,14 @@ namespace ts {
- related = isRelatedTo(s, t, reportErrors);
- }
- }
- + else if (variance === Variance.CoKey) {
- + related = isRelatedTo(filterType(s, t => !(t.flags & (TypeFlags.String | TypeFlags.Number))),
- + filterType(t, t => !(t.flags & (TypeFlags.String | TypeFlags.Number))), reportErrors);
- + }
- + else if (variance === Variance.ConKey) {
- + related = isRelatedTo(filterType(t, t => !(t.flags & (TypeFlags.String | TypeFlags.Number))),
- + filterType(s, t => !(t.flags & (TypeFlags.String | TypeFlags.Number))), reportErrors);
- + }
- else {
- // In the invariant case we first compare covariantly, and only when that
- // succeeds do we proceed to compare contravariantly. Thus, error elaboration
- @@ -12750,7 +12758,9 @@ namespace ts {
- if (target.flags & TypeFlags.TypeParameter) {
- // A source type { [P in Q]: X } is related to a target type T if keyof T is related to Q and X is related to T[Q].
- - if (getObjectFlags(source) & ObjectFlags.Mapped && isRelatedTo(getIndexType(target), getConstraintTypeFromMappedType(<MappedType>source))) {
- + if (getObjectFlags(source) & ObjectFlags.Mapped &&
- + isRelatedTo(instantiateType(getIndexType(target), reportKeyMarkers),
- + instantiateType(getConstraintTypeFromMappedType(<MappedType>source), reportKeyMarkers))) {
- if (!(getMappedTypeModifiers(<MappedType>source) & MappedTypeModifiers.IncludeOptional)) {
- const templateType = getTemplateTypeFromMappedType(<MappedType>source);
- const indexedAccessType = getIndexedAccessType(target, getTypeParameterFromMappedType(<MappedType>source));
- @@ -13012,7 +13022,14 @@ namespace ts {
- function reportUnmeasurableMarkers(p: TypeParameter) {
- if (unmeasurableMarkerHandler && (p === markerSuperType || p === markerSubType || p === markerOtherType)) {
- - unmeasurableMarkerHandler();
- + unmeasurableMarkerHandler(true);
- + }
- + return p;
- + }
- +
- + function reportKeyMarkers(p: TypeParameter) {
- + if (unmeasurableMarkerHandler && (p === markerSuperType || p === markerSubType || p === markerOtherType)) {
- + unmeasurableMarkerHandler(false, p.id);
- }
- return p;
- }
- @@ -13026,7 +13043,9 @@ namespace ts {
- if (modifiersRelated) {
- let result: Ternary;
- const targetConstraint = getConstraintTypeFromMappedType(target);
- - const sourceConstraint = instantiateType(getConstraintTypeFromMappedType(source), reportUnmeasurableMarkers);
- + const sourceConstraint = getMappedTypeModifiers(source) === 0 ?
- + instantiateType(getConstraintTypeFromMappedType(source), reportKeyMarkers) :
- + instantiateType(getConstraintTypeFromMappedType(source), reportUnmeasurableMarkers);
- if (result = isRelatedTo(targetConstraint, sourceConstraint, reportErrors)) {
- const mapper = createTypeMapper([getTypeParameterFromMappedType(source)], [getTypeParameterFromMappedType(target)]);
- return result & isRelatedTo(instantiateType(getTemplateTypeFromMappedType(source), mapper), getTemplateTypeFromMappedType(target), reportErrors);
- @@ -13503,8 +13522,17 @@ namespace ts {
- variances = [];
- for (const tp of typeParameters) {
- let unmeasurable = false;
- + let keyVars: Record<number, true> = {};
- const oldHandler = unmeasurableMarkerHandler;
- - unmeasurableMarkerHandler = () => unmeasurable = true;
- + unmeasurableMarkerHandler = (isUnmeasurable: boolean, id?: number) => {
- + if (isUnmeasurable) {
- + unmeasurable = true
- + }
- + else {
- + keyVars[id!] = true;
- + }
- +
- + };
- // We first compare instantiations where the type parameter is replaced with
- // marker types that have a known subtype relationship. From this we can infer
- // invariance, covariance, contravariance or bivariance.
- @@ -13520,6 +13548,17 @@ namespace ts {
- variance = Variance.Independent;
- }
- unmeasurableMarkerHandler = oldHandler;
- + if (keyVars[markerSubType.id] && keyVars[markerSuperType.id]) {
- + if (variance === Variance.Contravariant) {
- + variance = Variance.ConKey;
- + }
- + else if (variance === Variance.Covariant) {
- + variance = Variance.CoKey;
- + }
- + else {
- + unmeasurable = true;
- + }
- + }
- if (unmeasurable) {
- variance = Variance.Unmeasurable;
- const covariantID = getRelationKey(typeWithSub, typeWithSuper, assignableRelation);
- diff --git a/src/compiler/types.ts b/src/compiler/types.ts
- index b8a17470da..d1d3ff2254 100644
- --- a/src/compiler/types.ts
- +++ b/src/compiler/types.ts
- @@ -4133,7 +4133,9 @@ namespace ts {
- Contravariant = 2, // Contravariant
- Bivariant = 3, // Both covariant and contravariant
- Independent = 4, // Unwitnessed type parameter
- - Unmeasurable = 5, // Variance result is unusable - relationship relies on structural comparisons which are not reflected in generic relationships
- + Unmeasurable = 5, // Variance result is unusable - relationship relies on structural comparisons which are not reflected in generic relationships
- + CoKey = 6,
- + ConKey = 7
- }
- // Generic class and interface types
- --
- 2.17.2 (Apple Git-113)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement