@@ -2165,7 +2165,7 @@ namespace ts {
2165
2165
? "any"
2166
2166
: (<IntrinsicType>type).intrinsicName);
2167
2167
}
2168
- else if (type.flags & TypeFlags.ThisType ) {
2168
+ else if (type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType ) {
2169
2169
if (inObjectTypeLiteral) {
2170
2170
writer.reportInaccessibleThisError();
2171
2171
}
@@ -2183,9 +2183,14 @@ namespace ts {
2183
2183
// The specified symbol flags need to be reinterpreted as type flags
2184
2184
buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, nextFlags);
2185
2185
}
2186
- else if (!(flags & TypeFormatFlags.InTypeAlias) && type.flags & ( TypeFlags.Anonymous | TypeFlags.UnionOrIntersection) && type.aliasSymbol &&
2186
+ else if (!(flags & TypeFormatFlags.InTypeAlias) && (( type.flags & TypeFlags.Anonymous && !(<AnonymousType>type).target) || type.flags & TypeFlags.UnionOrIntersection) && type.aliasSymbol &&
2187
2187
isSymbolAccessible(type.aliasSymbol, enclosingDeclaration, SymbolFlags.Type, /*shouldComputeAliasesToMakeVisible*/ false).accessibility === SymbolAccessibility.Accessible) {
2188
- // Only write out inferred type with its corresponding type-alias if type-alias is visible
2188
+ // We emit inferred type as type-alias at the current localtion if all the following is true
2189
+ // the input type is has alias symbol that is accessible
2190
+ // the input type is a union, intersection or anonymous type that is fully instantiated (if not we want to keep dive into)
2191
+ // e.g.: export type Bar<X, Y> = () => [X, Y];
2192
+ // export type Foo<Y> = Bar<any, Y>;
2193
+ // export const y = (x: Foo<string>) => 1 // we want to emit as ...x: () => [any, string])
2189
2194
const typeArguments = type.aliasTypeArguments;
2190
2195
writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags);
2191
2196
}
@@ -3179,7 +3184,7 @@ namespace ts {
3179
3184
result.pattern = pattern;
3180
3185
}
3181
3186
if (hasComputedProperties) {
3182
- result.flags |= TypeFlags.ObjectLiteralPatternWithComputedProperties ;
3187
+ result.isObjectLiteralPatternWithComputedProperties = true ;
3183
3188
}
3184
3189
return result;
3185
3190
}
@@ -3766,7 +3771,8 @@ namespace ts {
3766
3771
(<GenericType>type).instantiations[getTypeListId(type.typeParameters)] = <GenericType>type;
3767
3772
(<GenericType>type).target = <GenericType>type;
3768
3773
(<GenericType>type).typeArguments = type.typeParameters;
3769
- type.thisType = <TypeParameter>createType(TypeFlags.TypeParameter | TypeFlags.ThisType);
3774
+ type.thisType = <TypeParameter>createType(TypeFlags.TypeParameter);
3775
+ type.thisType.isThisType = true;
3770
3776
type.thisType.symbol = symbol;
3771
3777
type.thisType.constraint = type;
3772
3778
}
@@ -4968,7 +4974,7 @@ namespace ts {
4968
4974
4969
4975
function hasConstraintReferenceTo(type: Type, target: TypeParameter): boolean {
4970
4976
let checked: Type[];
4971
- while (type && !( type.flags & TypeFlags.ThisType) && type.flags & TypeFlags. TypeParameter && !contains(checked, type)) {
4977
+ while (type && type.flags & TypeFlags.TypeParameter && !(( type as TypeParameter).isThisType) && !contains(checked, type)) {
4972
4978
if (type === target) {
4973
4979
return true;
4974
4980
}
@@ -5331,7 +5337,8 @@ namespace ts {
5331
5337
type.instantiations[getTypeListId(type.typeParameters)] = <GenericType>type;
5332
5338
type.target = <GenericType>type;
5333
5339
type.typeArguments = type.typeParameters;
5334
- type.thisType = <TypeParameter>createType(TypeFlags.TypeParameter | TypeFlags.ThisType);
5340
+ type.thisType = <TypeParameter>createType(TypeFlags.TypeParameter);
5341
+ type.thisType.isThisType = true;
5335
5342
type.thisType.constraint = type;
5336
5343
type.declaredProperties = properties;
5337
5344
type.declaredCallSignatures = emptyArray;
@@ -5444,7 +5451,26 @@ namespace ts {
5444
5451
return false;
5445
5452
}
5446
5453
5454
+ function isSetOfLiteralsFromSameEnum(types: TypeSet): boolean {
5455
+ const first = types[0];
5456
+ if (first.flags & TypeFlags.EnumLiteral) {
5457
+ const firstEnum = getParentOfSymbol(first.symbol);
5458
+ for (let i = 1; i < types.length; i++) {
5459
+ const other = types[i];
5460
+ if (!(other.flags & TypeFlags.EnumLiteral) || (firstEnum !== getParentOfSymbol(other.symbol))) {
5461
+ return false;
5462
+ }
5463
+ }
5464
+ return true;
5465
+ }
5466
+
5467
+ return false;
5468
+ }
5469
+
5447
5470
function removeSubtypes(types: TypeSet) {
5471
+ if (types.length === 0 || isSetOfLiteralsFromSameEnum(types)) {
5472
+ return;
5473
+ }
5448
5474
let i = types.length;
5449
5475
while (i > 0) {
5450
5476
i--;
@@ -6647,7 +6673,8 @@ namespace ts {
6647
6673
}
6648
6674
6649
6675
function hasExcessProperties(source: FreshObjectLiteralType, target: Type, reportErrors: boolean): boolean {
6650
- if (!(target.flags & TypeFlags.ObjectLiteralPatternWithComputedProperties) && maybeTypeOfKind(target, TypeFlags.ObjectType)) {
6676
+ if (maybeTypeOfKind(target, TypeFlags.ObjectType) &&
6677
+ (!(target.flags & TypeFlags.ObjectType) || !(target as ObjectType).isObjectLiteralPatternWithComputedProperties)) {
6651
6678
for (const prop of getPropertiesOfObjectType(source)) {
6652
6679
if (!isKnownProperty(target, prop.name)) {
6653
6680
if (reportErrors) {
@@ -9351,7 +9378,7 @@ namespace ts {
9351
9378
captureLexicalThis(node, container);
9352
9379
}
9353
9380
if (isFunctionLike(container) &&
9354
- (!isInParameterInitializerBeforeContainingFunction(node) || getFunctionLikeThisParameter (container))) {
9381
+ (!isInParameterInitializerBeforeContainingFunction(node) || getThisParameter (container))) {
9355
9382
// Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated.
9356
9383
9357
9384
// If this is a function in a JS file, it might be a class method. Check if it's the RHS
@@ -9753,7 +9780,7 @@ namespace ts {
9753
9780
// corresponding set accessor has a type annotation, return statements in the function are contextually typed
9754
9781
if (functionDecl.type ||
9755
9782
functionDecl.kind === SyntaxKind.Constructor ||
9756
- functionDecl.kind === SyntaxKind.GetAccessor && getSetAccessorTypeAnnotationNode(<AccessorDeclaration >getDeclarationOfKind(functionDecl.symbol, SyntaxKind.SetAccessor))) {
9783
+ functionDecl.kind === SyntaxKind.GetAccessor && getSetAccessorTypeAnnotationNode(<SetAccessorDeclaration >getDeclarationOfKind(functionDecl.symbol, SyntaxKind.SetAccessor))) {
9757
9784
return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl));
9758
9785
}
9759
9786
@@ -10307,7 +10334,8 @@ namespace ts {
10307
10334
patternWithComputedProperties = true;
10308
10335
}
10309
10336
}
10310
- else if (contextualTypeHasPattern && !(contextualType.flags & TypeFlags.ObjectLiteralPatternWithComputedProperties)) {
10337
+ else if (contextualTypeHasPattern &&
10338
+ !(contextualType.flags & TypeFlags.ObjectType && (contextualType as ObjectType).isObjectLiteralPatternWithComputedProperties)) {
10311
10339
// If object literal is contextually typed by the implied type of a binding pattern, and if the
10312
10340
// binding pattern specifies a default value for the property, make the property optional.
10313
10341
const impliedProp = getPropertyOfType(contextualType, member.name);
@@ -10372,7 +10400,10 @@ namespace ts {
10372
10400
const numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, propertiesArray, IndexKind.Number) : undefined;
10373
10401
const result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
10374
10402
const freshObjectLiteralFlag = compilerOptions.suppressExcessPropertyErrors ? 0 : TypeFlags.FreshLiteral;
10375
- result.flags |= TypeFlags.ObjectLiteral | TypeFlags.ContainsObjectLiteral | freshObjectLiteralFlag | (typeFlags & TypeFlags.PropagatingFlags) | (patternWithComputedProperties ? TypeFlags.ObjectLiteralPatternWithComputedProperties : 0);
10403
+ result.flags |= TypeFlags.ObjectLiteral | TypeFlags.ContainsObjectLiteral | freshObjectLiteralFlag | (typeFlags & TypeFlags.PropagatingFlags);
10404
+ if (patternWithComputedProperties) {
10405
+ result.isObjectLiteralPatternWithComputedProperties = true;
10406
+ }
10376
10407
if (inDestructuringPattern) {
10377
10408
result.pattern = node;
10378
10409
}
@@ -10942,7 +10973,7 @@ namespace ts {
10942
10973
return true;
10943
10974
}
10944
10975
// An instance property must be accessed through an instance of the enclosing class
10945
- if (type.flags & TypeFlags.ThisType ) {
10976
+ if (type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType ) {
10946
10977
// get the original type -- represented as the type constraint of the 'this' type
10947
10978
type = getConstraintOfTypeParameter(<TypeParameter>type);
10948
10979
}
@@ -10992,7 +11023,7 @@ namespace ts {
10992
11023
const prop = getPropertyOfType(apparentType, right.text);
10993
11024
if (!prop) {
10994
11025
if (right.text && !checkAndReportErrorForExtendingInterface(node)) {
10995
- reportNonexistentProperty(right, type.flags & TypeFlags.ThisType ? apparentType : type);
11026
+ reportNonexistentProperty(right, type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType ? apparentType : type);
10996
11027
}
10997
11028
return unknownType;
10998
11029
}
@@ -12740,7 +12771,10 @@ namespace ts {
12740
12771
if (!contextualSignature) {
12741
12772
reportErrorsFromWidening(func, type);
12742
12773
}
12743
- if (isUnitType(type) && !(contextualSignature && isLiteralContextualType(getReturnTypeOfSignature(contextualSignature)))) {
12774
+ if (isUnitType(type) &&
12775
+ !(contextualSignature &&
12776
+ isLiteralContextualType(
12777
+ contextualSignature === getSignatureFromDeclaration(func) ? type : getReturnTypeOfSignature(contextualSignature)))) {
12744
12778
type = getWidenedLiteralType(type);
12745
12779
}
12746
12780
@@ -15531,10 +15565,6 @@ namespace ts {
15531
15565
}
15532
15566
}
15533
15567
15534
- function parameterIsThisKeyword(parameter: ParameterDeclaration) {
15535
- return parameter.name && (<Identifier>parameter.name).originalKeywordKind === SyntaxKind.ThisKeyword;
15536
- }
15537
-
15538
15568
function parameterNameStartsWithUnderscore(parameter: ParameterDeclaration) {
15539
15569
return parameter.name && parameter.name.kind === SyntaxKind.Identifier && (<Identifier>parameter.name).text.charCodeAt(0) === CharacterCodes._;
15540
15570
}
@@ -16427,7 +16457,7 @@ namespace ts {
16427
16457
}
16428
16458
16429
16459
function isGetAccessorWithAnnotatedSetAccessor(node: FunctionLikeDeclaration) {
16430
- return !!(node.kind === SyntaxKind.GetAccessor && getSetAccessorTypeAnnotationNode(<AccessorDeclaration >getDeclarationOfKind(node.symbol, SyntaxKind.SetAccessor)));
16460
+ return !!(node.kind === SyntaxKind.GetAccessor && getSetAccessorTypeAnnotationNode(<SetAccessorDeclaration >getDeclarationOfKind(node.symbol, SyntaxKind.SetAccessor)));
16431
16461
}
16432
16462
16433
16463
function isUnwrappedReturnTypeVoidOrAny(func: FunctionLikeDeclaration, returnType: Type): boolean {
@@ -18432,6 +18462,9 @@ namespace ts {
18432
18462
(<ImportDeclaration>node.parent).moduleSpecifier === node)) {
18433
18463
return resolveExternalModuleName(node, <LiteralExpression>node);
18434
18464
}
18465
+ if (isInJavaScriptFile(node) && isRequireCall(node.parent, /*checkArgumentIsStringLiteral*/ false)) {
18466
+ return resolveExternalModuleName(node, <LiteralExpression>node);
18467
+ }
18435
18468
// Fall through
18436
18469
18437
18470
case SyntaxKind.NumericLiteral:
@@ -20117,18 +20150,8 @@ namespace ts {
20117
20150
}
20118
20151
20119
20152
function getAccessorThisParameter(accessor: AccessorDeclaration): ParameterDeclaration {
20120
- if (accessor.parameters.length === (accessor.kind === SyntaxKind.GetAccessor ? 1 : 2) &&
20121
- accessor.parameters[0].name.kind === SyntaxKind.Identifier &&
20122
- (<Identifier>accessor.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword) {
20123
- return accessor.parameters[0];
20124
- }
20125
- }
20126
-
20127
- function getFunctionLikeThisParameter(func: FunctionLikeDeclaration) {
20128
- if (func.parameters.length &&
20129
- func.parameters[0].name.kind === SyntaxKind.Identifier &&
20130
- (<Identifier>func.parameters[0].name).originalKeywordKind === SyntaxKind.ThisKeyword) {
20131
- return func.parameters[0];
20153
+ if (accessor.parameters.length === (accessor.kind === SyntaxKind.GetAccessor ? 1 : 2)) {
20154
+ return getThisParameter(accessor);
20132
20155
}
20133
20156
}
20134
20157
0 commit comments