Skip to content

Commit 28e3e6f

Browse files
TypeScript Botahejlsbergtypescript-bot
authored
🤖 Pick PR microsoft#44219 (Properly remove generic types that ...) into release-4.3 (microsoft#44243)
* Cherry-pick PR microsoft#44219 into release-4.3 Component commits: 4475012 Improve getNonNullableType function 86f09ae Add tests d45806a More closely match previous behavior 634b01a Add non-strict mode test * Update LKG Co-authored-by: Anders Hejlsberg <[email protected]> Co-authored-by: typescript-bot <[email protected]>
1 parent d6e6fa7 commit 28e3e6f

15 files changed

+460
-77
lines changed

‎lib/tsc.js

+18-11
Original file line numberDiff line numberDiff line change
@@ -46281,6 +46281,10 @@ var ts;
4628146281
var indexSymbol = symbol && getIndexSymbol(symbol);
4628246282
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
4628346283
}
46284+
function getIndexDeclarationOfSymbolTable(symbolTable, kind) {
46285+
var indexSymbol = symbolTable && getIndexSymbolFromSymbolTable(symbolTable);
46286+
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
46287+
}
4628446288
function getIndexDeclarationOfIndexSymbol(indexSymbol, kind) {
4628546289
var syntaxKind = kind === 1 ? 144 : 147;
4628646290
if (indexSymbol === null || indexSymbol === void 0 ? void 0 : indexSymbol.declarations) {
@@ -52614,13 +52618,13 @@ var ts;
5261452618
return type.flags & 32768 ? type : getUnionType([type, undefinedType]);
5261552619
}
5261652620
function getGlobalNonNullableTypeInstantiation(type) {
52621+
var reducedType = getTypeWithFacts(type, 2097152);
5261752622
if (!deferredGlobalNonNullableTypeAlias) {
5261852623
deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable", 524288, undefined) || unknownSymbol;
5261952624
}
52620-
if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) {
52621-
return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]);
52622-
}
52623-
return getTypeWithFacts(type, 2097152);
52625+
return deferredGlobalNonNullableTypeAlias !== unknownSymbol ?
52626+
getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [reducedType]) :
52627+
reducedType;
5262452628
}
5262552629
function getNonNullableType(type) {
5262652630
return strictNullChecks ? getGlobalNonNullableTypeInstantiation(type) : type;
@@ -55757,7 +55761,7 @@ var ts;
5575755761
!isGenericIndexType(getTypeOfExpression(parent.argumentExpression));
5575855762
}
5575955763
function isGenericTypeWithUnionConstraint(type) {
55760-
return !!(type.flags & 465829888 && getBaseConstraintOrType(type).flags & 1048576);
55764+
return !!(type.flags & 465829888 && getBaseConstraintOrType(type).flags & (98304 | 1048576));
5576155765
}
5576255766
function containsGenericType(type) {
5576355767
return !!(type.flags & 465829888 || type.flags & 3145728 && ts.some(type.types, containsGenericType));
@@ -65459,21 +65463,24 @@ var ts;
6545965463
checkBlock(node.finallyBlock);
6546065464
}
6546165465
}
65462-
function checkIndexConstraints(type) {
65463-
var declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, 1);
65464-
var declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, 0);
65466+
function checkIndexConstraints(type, isStatic) {
65467+
var _a, _b, _c, _d;
65468+
var declaredNumberIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_a = type.symbol) === null || _a === void 0 ? void 0 : _a.exports : (_b = type.symbol) === null || _b === void 0 ? void 0 : _b.members, 1);
65469+
var declaredStringIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_c = type.symbol) === null || _c === void 0 ? void 0 : _c.exports : (_d = type.symbol) === null || _d === void 0 ? void 0 : _d.members, 0);
6546565470
var stringIndexType = getIndexTypeOfType(type, 0);
6546665471
var numberIndexType = getIndexTypeOfType(type, 1);
6546765472
if (stringIndexType || numberIndexType) {
6546865473
ts.forEach(getPropertiesOfObjectType(type), function (prop) {
65474+
if (isStatic && prop.flags & 4194304)
65475+
return;
6546965476
var propType = getTypeOfSymbol(prop);
6547065477
checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, 0);
6547165478
checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, 1);
6547265479
});
6547365480
var classDeclaration = type.symbol.valueDeclaration;
6547465481
if (ts.getObjectFlags(type) & 1 && classDeclaration && ts.isClassLike(classDeclaration)) {
65475-
for (var _i = 0, _a = classDeclaration.members; _i < _a.length; _i++) {
65476-
var member = _a[_i];
65482+
for (var _i = 0, _e = classDeclaration.members; _i < _e.length; _i++) {
65483+
var member = _e[_i];
6547765484
if (!ts.hasSyntacticModifier(member, 32) && !hasBindableName(member)) {
6547865485
var symbol = getSymbolOfNode(member);
6547965486
var propType = getTypeOfSymbol(symbol);
@@ -65767,7 +65774,7 @@ var ts;
6576765774
}
6576865775
if (produceDiagnostics) {
6576965776
checkIndexConstraints(type);
65770-
checkIndexConstraints(staticType);
65777+
checkIndexConstraints(staticType, true);
6577165778
checkTypeForDuplicateIndexSignatures(node);
6577265779
checkPropertyInitialization(node);
6577365780
}

‎lib/tsserver.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -56010,6 +56010,10 @@ var ts;
5601056010
var indexSymbol = symbol && getIndexSymbol(symbol);
5601156011
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
5601256012
}
56013+
function getIndexDeclarationOfSymbolTable(symbolTable, kind) {
56014+
var indexSymbol = symbolTable && getIndexSymbolFromSymbolTable(symbolTable);
56015+
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
56016+
}
5601356017
function getIndexDeclarationOfIndexSymbol(indexSymbol, kind) {
5601456018
var syntaxKind = kind === 1 /* Number */ ? 144 /* NumberKeyword */ : 147 /* StringKeyword */;
5601556019
if (indexSymbol === null || indexSymbol === void 0 ? void 0 : indexSymbol.declarations) {
@@ -63218,14 +63222,17 @@ var ts;
6321863222
return type.flags & 32768 /* Undefined */ ? type : getUnionType([type, undefinedType]);
6321963223
}
6322063224
function getGlobalNonNullableTypeInstantiation(type) {
63225+
// First reduce away any constituents that are assignable to 'undefined' or 'null'. This not only eliminates
63226+
// 'undefined' and 'null', but also higher-order types such as a type parameter 'U extends undefined | null'
63227+
// that isn't eliminated by a NonNullable<T> instantiation.
63228+
var reducedType = getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
6322163229
if (!deferredGlobalNonNullableTypeAlias) {
6322263230
deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable", 524288 /* TypeAlias */, /*diagnostic*/ undefined) || unknownSymbol;
6322363231
}
63224-
// Use NonNullable global type alias if available to improve quick info/declaration emit
63225-
if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) {
63226-
return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]);
63227-
}
63228-
return getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */); // Type alias unavailable, fall back to non-higher-order behavior
63232+
// If the NonNullable<T> type is available, return an instantiation. Otherwise just return the reduced type.
63233+
return deferredGlobalNonNullableTypeAlias !== unknownSymbol ?
63234+
getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [reducedType]) :
63235+
reducedType;
6322963236
}
6323063237
function getNonNullableType(type) {
6323163238
return strictNullChecks ? getGlobalNonNullableTypeInstantiation(type) : type;
@@ -66813,7 +66820,7 @@ var ts;
6681366820
!isGenericIndexType(getTypeOfExpression(parent.argumentExpression));
6681466821
}
6681566822
function isGenericTypeWithUnionConstraint(type) {
66816-
return !!(type.flags & 465829888 /* Instantiable */ && getBaseConstraintOrType(type).flags & 1048576 /* Union */);
66823+
return !!(type.flags & 465829888 /* Instantiable */ && getBaseConstraintOrType(type).flags & (98304 /* Nullable */ | 1048576 /* Union */));
6681766824
}
6681866825
function containsGenericType(type) {
6681966826
return !!(type.flags & 465829888 /* Instantiable */ || type.flags & 3145728 /* UnionOrIntersection */ && ts.some(type.types, containsGenericType));
@@ -78287,21 +78294,24 @@ var ts;
7828778294
checkBlock(node.finallyBlock);
7828878295
}
7828978296
}
78290-
function checkIndexConstraints(type) {
78291-
var declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, 1 /* Number */);
78292-
var declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, 0 /* String */);
78297+
function checkIndexConstraints(type, isStatic) {
78298+
var _a, _b, _c, _d;
78299+
var declaredNumberIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_a = type.symbol) === null || _a === void 0 ? void 0 : _a.exports : (_b = type.symbol) === null || _b === void 0 ? void 0 : _b.members, 1 /* Number */);
78300+
var declaredStringIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_c = type.symbol) === null || _c === void 0 ? void 0 : _c.exports : (_d = type.symbol) === null || _d === void 0 ? void 0 : _d.members, 0 /* String */);
7829378301
var stringIndexType = getIndexTypeOfType(type, 0 /* String */);
7829478302
var numberIndexType = getIndexTypeOfType(type, 1 /* Number */);
7829578303
if (stringIndexType || numberIndexType) {
7829678304
ts.forEach(getPropertiesOfObjectType(type), function (prop) {
78305+
if (isStatic && prop.flags & 4194304 /* Prototype */)
78306+
return;
7829778307
var propType = getTypeOfSymbol(prop);
7829878308
checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */);
7829978309
checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */);
7830078310
});
7830178311
var classDeclaration = type.symbol.valueDeclaration;
7830278312
if (ts.getObjectFlags(type) & 1 /* Class */ && classDeclaration && ts.isClassLike(classDeclaration)) {
78303-
for (var _i = 0, _a = classDeclaration.members; _i < _a.length; _i++) {
78304-
var member = _a[_i];
78313+
for (var _i = 0, _e = classDeclaration.members; _i < _e.length; _i++) {
78314+
var member = _e[_i];
7830578315
// Only process instance properties with computed names here.
7830678316
// Static properties cannot be in conflict with indexers,
7830778317
// and properties with literal names were already checked.
@@ -78631,7 +78641,7 @@ var ts;
7863178641
}
7863278642
if (produceDiagnostics) {
7863378643
checkIndexConstraints(type);
78634-
checkIndexConstraints(staticType);
78644+
checkIndexConstraints(staticType, /*isStatic*/ true);
7863578645
checkTypeForDuplicateIndexSignatures(node);
7863678646
checkPropertyInitialization(node);
7863778647
}

‎lib/tsserverlibrary.js

+22-12
Original file line numberDiff line numberDiff line change
@@ -56204,6 +56204,10 @@ var ts;
5620456204
var indexSymbol = symbol && getIndexSymbol(symbol);
5620556205
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
5620656206
}
56207+
function getIndexDeclarationOfSymbolTable(symbolTable, kind) {
56208+
var indexSymbol = symbolTable && getIndexSymbolFromSymbolTable(symbolTable);
56209+
return indexSymbol && getIndexDeclarationOfIndexSymbol(indexSymbol, kind);
56210+
}
5620756211
function getIndexDeclarationOfIndexSymbol(indexSymbol, kind) {
5620856212
var syntaxKind = kind === 1 /* Number */ ? 144 /* NumberKeyword */ : 147 /* StringKeyword */;
5620956213
if (indexSymbol === null || indexSymbol === void 0 ? void 0 : indexSymbol.declarations) {
@@ -63412,14 +63416,17 @@ var ts;
6341263416
return type.flags & 32768 /* Undefined */ ? type : getUnionType([type, undefinedType]);
6341363417
}
6341463418
function getGlobalNonNullableTypeInstantiation(type) {
63419+
// First reduce away any constituents that are assignable to 'undefined' or 'null'. This not only eliminates
63420+
// 'undefined' and 'null', but also higher-order types such as a type parameter 'U extends undefined | null'
63421+
// that isn't eliminated by a NonNullable<T> instantiation.
63422+
var reducedType = getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */);
6341563423
if (!deferredGlobalNonNullableTypeAlias) {
6341663424
deferredGlobalNonNullableTypeAlias = getGlobalSymbol("NonNullable", 524288 /* TypeAlias */, /*diagnostic*/ undefined) || unknownSymbol;
6341763425
}
63418-
// Use NonNullable global type alias if available to improve quick info/declaration emit
63419-
if (deferredGlobalNonNullableTypeAlias !== unknownSymbol) {
63420-
return getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [type]);
63421-
}
63422-
return getTypeWithFacts(type, 2097152 /* NEUndefinedOrNull */); // Type alias unavailable, fall back to non-higher-order behavior
63426+
// If the NonNullable<T> type is available, return an instantiation. Otherwise just return the reduced type.
63427+
return deferredGlobalNonNullableTypeAlias !== unknownSymbol ?
63428+
getTypeAliasInstantiation(deferredGlobalNonNullableTypeAlias, [reducedType]) :
63429+
reducedType;
6342363430
}
6342463431
function getNonNullableType(type) {
6342563432
return strictNullChecks ? getGlobalNonNullableTypeInstantiation(type) : type;
@@ -67007,7 +67014,7 @@ var ts;
6700767014
!isGenericIndexType(getTypeOfExpression(parent.argumentExpression));
6700867015
}
6700967016
function isGenericTypeWithUnionConstraint(type) {
67010-
return !!(type.flags & 465829888 /* Instantiable */ && getBaseConstraintOrType(type).flags & 1048576 /* Union */);
67017+
return !!(type.flags & 465829888 /* Instantiable */ && getBaseConstraintOrType(type).flags & (98304 /* Nullable */ | 1048576 /* Union */));
6701167018
}
6701267019
function containsGenericType(type) {
6701367020
return !!(type.flags & 465829888 /* Instantiable */ || type.flags & 3145728 /* UnionOrIntersection */ && ts.some(type.types, containsGenericType));
@@ -78481,21 +78488,24 @@ var ts;
7848178488
checkBlock(node.finallyBlock);
7848278489
}
7848378490
}
78484-
function checkIndexConstraints(type) {
78485-
var declaredNumberIndexer = getIndexDeclarationOfSymbol(type.symbol, 1 /* Number */);
78486-
var declaredStringIndexer = getIndexDeclarationOfSymbol(type.symbol, 0 /* String */);
78491+
function checkIndexConstraints(type, isStatic) {
78492+
var _a, _b, _c, _d;
78493+
var declaredNumberIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_a = type.symbol) === null || _a === void 0 ? void 0 : _a.exports : (_b = type.symbol) === null || _b === void 0 ? void 0 : _b.members, 1 /* Number */);
78494+
var declaredStringIndexer = getIndexDeclarationOfSymbolTable(isStatic ? (_c = type.symbol) === null || _c === void 0 ? void 0 : _c.exports : (_d = type.symbol) === null || _d === void 0 ? void 0 : _d.members, 0 /* String */);
7848778495
var stringIndexType = getIndexTypeOfType(type, 0 /* String */);
7848878496
var numberIndexType = getIndexTypeOfType(type, 1 /* Number */);
7848978497
if (stringIndexType || numberIndexType) {
7849078498
ts.forEach(getPropertiesOfObjectType(type), function (prop) {
78499+
if (isStatic && prop.flags & 4194304 /* Prototype */)
78500+
return;
7849178501
var propType = getTypeOfSymbol(prop);
7849278502
checkIndexConstraintForProperty(prop, propType, type, declaredStringIndexer, stringIndexType, 0 /* String */);
7849378503
checkIndexConstraintForProperty(prop, propType, type, declaredNumberIndexer, numberIndexType, 1 /* Number */);
7849478504
});
7849578505
var classDeclaration = type.symbol.valueDeclaration;
7849678506
if (ts.getObjectFlags(type) & 1 /* Class */ && classDeclaration && ts.isClassLike(classDeclaration)) {
78497-
for (var _i = 0, _a = classDeclaration.members; _i < _a.length; _i++) {
78498-
var member = _a[_i];
78507+
for (var _i = 0, _e = classDeclaration.members; _i < _e.length; _i++) {
78508+
var member = _e[_i];
7849978509
// Only process instance properties with computed names here.
7850078510
// Static properties cannot be in conflict with indexers,
7850178511
// and properties with literal names were already checked.
@@ -78825,7 +78835,7 @@ var ts;
7882578835
}
7882678836
if (produceDiagnostics) {
7882778837
checkIndexConstraints(type);
78828-
checkIndexConstraints(staticType);
78838+
checkIndexConstraints(staticType, /*isStatic*/ true);
7882978839
checkTypeForDuplicateIndexSignatures(node);
7883078840
checkPropertyInitialization(node);
7883178841
}

0 commit comments

Comments
 (0)