@@ -2165,7 +2165,7 @@ namespace ts {
21652165 ? "any"
21662166 : (<IntrinsicType>type).intrinsicName);
21672167 }
2168- else if (type.flags & TypeFlags.ThisType ) {
2168+ else if (type.flags & TypeFlags.TypeParameter && (type as TypeParameter).isThisType ) {
21692169 if (inObjectTypeLiteral) {
21702170 writer.reportInaccessibleThisError();
21712171 }
@@ -2183,9 +2183,14 @@ namespace ts {
21832183 // The specified symbol flags need to be reinterpreted as type flags
21842184 buildSymbolDisplay(type.symbol, writer, enclosingDeclaration, SymbolFlags.Type, SymbolFormatFlags.None, nextFlags);
21852185 }
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 &&
21872187 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])
21892194 const typeArguments = type.aliasTypeArguments;
21902195 writeSymbolTypeReference(type.aliasSymbol, typeArguments, 0, typeArguments ? typeArguments.length : 0, nextFlags);
21912196 }
@@ -3179,7 +3184,7 @@ namespace ts {
31793184 result.pattern = pattern;
31803185 }
31813186 if (hasComputedProperties) {
3182- result.flags |= TypeFlags.ObjectLiteralPatternWithComputedProperties ;
3187+ result.isObjectLiteralPatternWithComputedProperties = true ;
31833188 }
31843189 return result;
31853190 }
@@ -3766,7 +3771,8 @@ namespace ts {
37663771 (<GenericType>type).instantiations[getTypeListId(type.typeParameters)] = <GenericType>type;
37673772 (<GenericType>type).target = <GenericType>type;
37683773 (<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;
37703776 type.thisType.symbol = symbol;
37713777 type.thisType.constraint = type;
37723778 }
@@ -4968,7 +4974,7 @@ namespace ts {
49684974
49694975 function hasConstraintReferenceTo(type: Type, target: TypeParameter): boolean {
49704976 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)) {
49724978 if (type === target) {
49734979 return true;
49744980 }
@@ -5331,7 +5337,8 @@ namespace ts {
53315337 type.instantiations[getTypeListId(type.typeParameters)] = <GenericType>type;
53325338 type.target = <GenericType>type;
53335339 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;
53355342 type.thisType.constraint = type;
53365343 type.declaredProperties = properties;
53375344 type.declaredCallSignatures = emptyArray;
@@ -5444,7 +5451,26 @@ namespace ts {
54445451 return false;
54455452 }
54465453
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+
54475470 function removeSubtypes(types: TypeSet) {
5471+ if (types.length === 0 || isSetOfLiteralsFromSameEnum(types)) {
5472+ return;
5473+ }
54485474 let i = types.length;
54495475 while (i > 0) {
54505476 i--;
@@ -6647,7 +6673,8 @@ namespace ts {
66476673 }
66486674
66496675 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)) {
66516678 for (const prop of getPropertiesOfObjectType(source)) {
66526679 if (!isKnownProperty(target, prop.name)) {
66536680 if (reportErrors) {
@@ -9351,7 +9378,7 @@ namespace ts {
93519378 captureLexicalThis(node, container);
93529379 }
93539380 if (isFunctionLike(container) &&
9354- (!isInParameterInitializerBeforeContainingFunction(node) || getFunctionLikeThisParameter (container))) {
9381+ (!isInParameterInitializerBeforeContainingFunction(node) || getThisParameter (container))) {
93559382 // Note: a parameter initializer should refer to class-this unless function-this is explicitly annotated.
93569383
93579384 // 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 {
97539780 // corresponding set accessor has a type annotation, return statements in the function are contextually typed
97549781 if (functionDecl.type ||
97559782 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))) {
97579784 return getReturnTypeOfSignature(getSignatureFromDeclaration(functionDecl));
97589785 }
97599786
@@ -10307,7 +10334,8 @@ namespace ts {
1030710334 patternWithComputedProperties = true;
1030810335 }
1030910336 }
10310- else if (contextualTypeHasPattern && !(contextualType.flags & TypeFlags.ObjectLiteralPatternWithComputedProperties)) {
10337+ else if (contextualTypeHasPattern &&
10338+ !(contextualType.flags & TypeFlags.ObjectType && (contextualType as ObjectType).isObjectLiteralPatternWithComputedProperties)) {
1031110339 // If object literal is contextually typed by the implied type of a binding pattern, and if the
1031210340 // binding pattern specifies a default value for the property, make the property optional.
1031310341 const impliedProp = getPropertyOfType(contextualType, member.name);
@@ -10372,7 +10400,10 @@ namespace ts {
1037210400 const numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, propertiesArray, IndexKind.Number) : undefined;
1037310401 const result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
1037410402 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+ }
1037610407 if (inDestructuringPattern) {
1037710408 result.pattern = node;
1037810409 }
@@ -10942,7 +10973,7 @@ namespace ts {
1094210973 return true;
1094310974 }
1094410975 // 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 ) {
1094610977 // get the original type -- represented as the type constraint of the 'this' type
1094710978 type = getConstraintOfTypeParameter(<TypeParameter>type);
1094810979 }
@@ -10992,7 +11023,7 @@ namespace ts {
1099211023 const prop = getPropertyOfType(apparentType, right.text);
1099311024 if (!prop) {
1099411025 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);
1099611027 }
1099711028 return unknownType;
1099811029 }
@@ -12740,7 +12771,10 @@ namespace ts {
1274012771 if (!contextualSignature) {
1274112772 reportErrorsFromWidening(func, type);
1274212773 }
12743- if (isUnitType(type) && !(contextualSignature && isLiteralContextualType(getReturnTypeOfSignature(contextualSignature)))) {
12774+ if (isUnitType(type) &&
12775+ !(contextualSignature &&
12776+ isLiteralContextualType(
12777+ contextualSignature === getSignatureFromDeclaration(func) ? type : getReturnTypeOfSignature(contextualSignature)))) {
1274412778 type = getWidenedLiteralType(type);
1274512779 }
1274612780
@@ -15531,10 +15565,6 @@ namespace ts {
1553115565 }
1553215566 }
1553315567
15534- function parameterIsThisKeyword(parameter: ParameterDeclaration) {
15535- return parameter.name && (<Identifier>parameter.name).originalKeywordKind === SyntaxKind.ThisKeyword;
15536- }
15537-
1553815568 function parameterNameStartsWithUnderscore(parameter: ParameterDeclaration) {
1553915569 return parameter.name && parameter.name.kind === SyntaxKind.Identifier && (<Identifier>parameter.name).text.charCodeAt(0) === CharacterCodes._;
1554015570 }
@@ -16427,7 +16457,7 @@ namespace ts {
1642716457 }
1642816458
1642916459 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)));
1643116461 }
1643216462
1643316463 function isUnwrappedReturnTypeVoidOrAny(func: FunctionLikeDeclaration, returnType: Type): boolean {
@@ -18432,6 +18462,9 @@ namespace ts {
1843218462 (<ImportDeclaration>node.parent).moduleSpecifier === node)) {
1843318463 return resolveExternalModuleName(node, <LiteralExpression>node);
1843418464 }
18465+ if (isInJavaScriptFile(node) && isRequireCall(node.parent, /*checkArgumentIsStringLiteral*/ false)) {
18466+ return resolveExternalModuleName(node, <LiteralExpression>node);
18467+ }
1843518468 // Fall through
1843618469
1843718470 case SyntaxKind.NumericLiteral:
@@ -20117,18 +20150,8 @@ namespace ts {
2011720150 }
2011820151
2011920152 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);
2013220155 }
2013320156 }
2013420157
0 commit comments