@@ -3003,7 +3003,7 @@ namespace ts {
30033003 (<GenericType>type).typeArguments = type.typeParameters;
30043004 type.thisType = <TypeParameter>createType(TypeFlags.TypeParameter | TypeFlags.ThisType);
30053005 type.thisType.symbol = symbol;
3006- type.thisType.constraint = getTypeWithThisArgument( type) ;
3006+ type.thisType.constraint = type;
30073007 }
30083008 }
30093009 return <InterfaceType>links.declaredType;
@@ -3546,19 +3546,29 @@ namespace ts {
35463546 return type.flags & TypeFlags.UnionOrIntersection ? getPropertiesOfUnionOrIntersectionType(<UnionType>type) : getPropertiesOfObjectType(type);
35473547 }
35483548
3549+ /**
3550+ * The apparent type of a type parameter is the base constraint instantiated with the type parameter
3551+ * as the type argument for the 'this' type.
3552+ */
3553+ function getApparentTypeOfTypeParameter(type: TypeParameter) {
3554+ if (!type.resolvedApparentType) {
3555+ let constraintType = getConstraintOfTypeParameter(type);
3556+ while (constraintType && constraintType.flags & TypeFlags.TypeParameter) {
3557+ constraintType = getConstraintOfTypeParameter(<TypeParameter>constraintType);
3558+ }
3559+ type.resolvedApparentType = getTypeWithThisArgument(constraintType || emptyObjectType, type);
3560+ }
3561+ return type.resolvedApparentType;
3562+ }
3563+
35493564 /**
35503565 * For a type parameter, return the base constraint of the type parameter. For the string, number,
35513566 * boolean, and symbol primitive types, return the corresponding object types. Otherwise return the
35523567 * type itself. Note that the apparent type of a union type is the union type itself.
35533568 */
35543569 function getApparentType(type: Type): Type {
35553570 if (type.flags & TypeFlags.TypeParameter) {
3556- do {
3557- type = getConstraintOfTypeParameter(<TypeParameter>type);
3558- } while (type && type.flags & TypeFlags.TypeParameter);
3559- if (!type) {
3560- type = emptyObjectType;
3561- }
3571+ type = getApparentTypeOfTypeParameter(<TypeParameter>type);
35623572 }
35633573 if (type.flags & TypeFlags.StringLike) {
35643574 type = globalStringType;
@@ -5099,9 +5109,6 @@ namespace ts {
50995109 }
51005110
51015111 function typeParameterIdenticalTo(source: TypeParameter, target: TypeParameter): Ternary {
5102- if (source.symbol.name !== target.symbol.name) {
5103- return Ternary.False;
5104- }
51055112 // covers case when both type parameters does not have constraint (both equal to noConstraintType)
51065113 if (source.constraint === target.constraint) {
51075114 return Ternary.True;
@@ -6462,9 +6469,10 @@ namespace ts {
64626469
64636470 function narrowTypeByInstanceof(type: Type, expr: BinaryExpression, assumeTrue: boolean): Type {
64646471 // Check that type is not any, assumed result is true, and we have variable symbol on the left
6465- if (isTypeAny(type) || !assumeTrue || expr.left.kind !== SyntaxKind.Identifier || getResolvedSymbol(<Identifier>expr.left) !== symbol) {
6472+ if (isTypeAny(type) || expr.left.kind !== SyntaxKind.Identifier || getResolvedSymbol(<Identifier>expr.left) !== symbol) {
64666473 return type;
64676474 }
6475+
64686476 // Check that right operand is a function type with a prototype property
64696477 const rightType = checkExpression(expr.right);
64706478 if (!isTypeSubtypeOf(rightType, globalFunctionType)) {
@@ -6496,6 +6504,13 @@ namespace ts {
64966504 }
64976505
64986506 if (targetType) {
6507+ if (!assumeTrue) {
6508+ if (type.flags & TypeFlags.Union) {
6509+ return getUnionType(filter((<UnionType>type).types, t => !isTypeSubtypeOf(t, targetType)));
6510+ }
6511+ return type;
6512+ }
6513+
64996514 return getNarrowedType(type, targetType);
65006515 }
65016516
@@ -14944,10 +14959,20 @@ namespace ts {
1494414959 getReferencedValueDeclaration,
1494514960 getTypeReferenceSerializationKind,
1494614961 isOptionalParameter,
14947- isArgumentsLocalBinding
14962+ isArgumentsLocalBinding,
14963+ getExternalModuleFileFromDeclaration
1494814964 };
1494914965 }
1495014966
14967+ function getExternalModuleFileFromDeclaration(declaration: ImportEqualsDeclaration | ImportDeclaration | ExportDeclaration): SourceFile {
14968+ const specifier = getExternalModuleName(declaration);
14969+ const moduleSymbol = getSymbolAtLocation(specifier);
14970+ if (!moduleSymbol) {
14971+ return undefined;
14972+ }
14973+ return getDeclarationOfKind(moduleSymbol, SyntaxKind.SourceFile) as SourceFile;
14974+ }
14975+
1495114976 function initializeTypeChecker() {
1495214977 // Bind all source files and propagate errors
1495314978 forEach(host.getSourceFiles(), file => {
0 commit comments