@@ -77,6 +77,7 @@ namespace ts {
77
77
const allowSyntheticDefaultImports = getAllowSyntheticDefaultImports(compilerOptions);
78
78
const strictNullChecks = getStrictOptionValue(compilerOptions, "strictNullChecks");
79
79
const strictFunctionTypes = getStrictOptionValue(compilerOptions, "strictFunctionTypes");
80
+ const strictBindCallApply = getStrictOptionValue(compilerOptions, "strictBindCallApply");
80
81
const strictPropertyInitialization = getStrictOptionValue(compilerOptions, "strictPropertyInitialization");
81
82
const noImplicitAny = getStrictOptionValue(compilerOptions, "noImplicitAny");
82
83
const noImplicitThis = getStrictOptionValue(compilerOptions, "noImplicitThis");
@@ -476,6 +477,8 @@ namespace ts {
476
477
477
478
let globalObjectType: ObjectType;
478
479
let globalFunctionType: ObjectType;
480
+ let globalCallableFunctionType: ObjectType;
481
+ let globalNewableFunctionType: ObjectType;
479
482
let globalArrayType: GenericType;
480
483
let globalReadonlyArrayType: GenericType;
481
484
let globalStringType: ObjectType;
@@ -1708,7 +1711,9 @@ namespace ts {
1708
1711
function checkResolvedBlockScopedVariable(result: Symbol, errorLocation: Node): void {
1709
1712
Debug.assert(!!(result.flags & SymbolFlags.BlockScopedVariable || result.flags & SymbolFlags.Class || result.flags & SymbolFlags.Enum));
1710
1713
// Block-scoped variables cannot be used before their definition
1711
- const declaration = forEach(result.declarations, d => isBlockOrCatchScoped(d) || isClassLike(d) || (d.kind === SyntaxKind.EnumDeclaration) ? d : undefined);
1714
+ const declaration = find(
1715
+ result.declarations,
1716
+ d => isBlockOrCatchScoped(d) || isClassLike(d) || (d.kind === SyntaxKind.EnumDeclaration) || isInJSFile(d) && !!getJSDocEnumTag(d));
1712
1717
1713
1718
if (declaration === undefined) return Debug.fail("Declaration to checkResolvedBlockScopedVariable is undefined");
1714
1719
@@ -7391,8 +7396,12 @@ namespace ts {
7391
7396
if (symbol && symbolIsValue(symbol)) {
7392
7397
return symbol;
7393
7398
}
7394
- if (resolved === anyFunctionType || resolved.callSignatures.length || resolved.constructSignatures.length) {
7395
- const symbol = getPropertyOfObjectType(globalFunctionType, name);
7399
+ const functionType = resolved === anyFunctionType ? globalFunctionType :
7400
+ resolved.callSignatures.length ? globalCallableFunctionType :
7401
+ resolved.constructSignatures.length ? globalNewableFunctionType :
7402
+ undefined;
7403
+ if (functionType) {
7404
+ const symbol = getPropertyOfObjectType(functionType, name);
7396
7405
if (symbol) {
7397
7406
return symbol;
7398
7407
}
@@ -13250,10 +13259,8 @@ namespace ts {
13250
13259
const targetCount = getParameterCount(target);
13251
13260
const sourceRestType = getEffectiveRestType(source);
13252
13261
const targetRestType = getEffectiveRestType(target);
13253
- const paramCount = targetRestType ? Math.min(targetCount - 1, sourceCount) :
13254
- sourceRestType ? targetCount :
13255
- Math.min(sourceCount, targetCount);
13256
-
13262
+ const targetNonRestCount = targetRestType ? targetCount - 1 : targetCount;
13263
+ const paramCount = sourceRestType ? targetNonRestCount : Math.min(sourceCount, targetNonRestCount);
13257
13264
const sourceThisType = getThisTypeOfSignature(source);
13258
13265
if (sourceThisType) {
13259
13266
const targetThisType = getThisTypeOfSignature(target);
@@ -13900,15 +13907,17 @@ namespace ts {
13900
13907
if (!inferredType) {
13901
13908
const signature = context.signature;
13902
13909
if (signature) {
13903
- if (inference.contraCandidates && (!inference.candidates || inference.candidates.length === 1 && inference.candidates[0].flags & TypeFlags.Never)) {
13904
- // If we have contravariant inferences, but no covariant inferences or a single
13905
- // covariant inference of 'never', we find the best common subtype and treat that
13906
- // as a single covariant candidate.
13907
- inference.candidates = [getContravariantInference(inference)];
13908
- inference.contraCandidates = undefined;
13909
- }
13910
- if (inference.candidates) {
13911
- inferredType = getCovariantInference(inference, signature);
13910
+ const inferredCovariantType = inference.candidates ? getCovariantInference(inference, signature) : undefined;
13911
+ if (inference.contraCandidates) {
13912
+ const inferredContravariantType = getContravariantInference(inference);
13913
+ // If we have both co- and contra-variant inferences, we prefer the contra-variant inference
13914
+ // unless the co-variant inference is a subtype and not 'never'.
13915
+ inferredType = inferredCovariantType && !(inferredCovariantType.flags & TypeFlags.Never) &&
13916
+ isTypeSubtypeOf(inferredCovariantType, inferredContravariantType) ?
13917
+ inferredCovariantType : inferredContravariantType;
13918
+ }
13919
+ else if (inferredCovariantType) {
13920
+ inferredType = inferredCovariantType;
13912
13921
}
13913
13922
else if (context.flags & InferenceFlags.NoDefault) {
13914
13923
// We use silentNeverType as the wildcard that signals no inferences.
@@ -19684,7 +19693,10 @@ namespace ts {
19684
19693
checkCandidate = candidate;
19685
19694
}
19686
19695
if (!checkApplicableSignature(node, args, checkCandidate, relation, excludeArgument, /*reportErrors*/ false)) {
19687
- candidateForArgumentError = checkCandidate;
19696
+ // Give preference to error candidates that have no rest parameters (as they are more specific)
19697
+ if (!candidateForArgumentError || getEffectiveRestType(candidateForArgumentError) || !getEffectiveRestType(checkCandidate)) {
19698
+ candidateForArgumentError = checkCandidate;
19699
+ }
19688
19700
continue;
19689
19701
}
19690
19702
if (excludeArgument) {
@@ -19697,7 +19709,10 @@ namespace ts {
19697
19709
checkCandidate = getSignatureInstantiation(candidate, typeArgumentTypes, isInJSFile(candidate.declaration));
19698
19710
}
19699
19711
if (!checkApplicableSignature(node, args, checkCandidate, relation, excludeArgument, /*reportErrors*/ false)) {
19700
- candidateForArgumentError = checkCandidate;
19712
+ // Give preference to error candidates that have no rest parameters (as they are more specific)
19713
+ if (!candidateForArgumentError || getEffectiveRestType(candidateForArgumentError) || !getEffectiveRestType(checkCandidate)) {
19714
+ candidateForArgumentError = checkCandidate;
19715
+ }
19701
19716
continue;
19702
19717
}
19703
19718
}
@@ -28029,8 +28044,11 @@ namespace ts {
28029
28044
function getAugmentedPropertiesOfType(type: Type): Symbol[] {
28030
28045
type = getApparentType(type);
28031
28046
const propsByName = createSymbolTable(getPropertiesOfType(type));
28032
- if (typeHasCallOrConstructSignatures(type)) {
28033
- forEach(getPropertiesOfType(globalFunctionType), p => {
28047
+ const functionType = getSignaturesOfType(type, SignatureKind.Call).length ? globalCallableFunctionType :
28048
+ getSignaturesOfType(type, SignatureKind.Construct).length ? globalNewableFunctionType :
28049
+ undefined;
28050
+ if (functionType) {
28051
+ forEach(getPropertiesOfType(functionType), p => {
28034
28052
if (!propsByName.has(p.escapedName)) {
28035
28053
propsByName.set(p.escapedName, p);
28036
28054
}
@@ -28810,6 +28828,8 @@ namespace ts {
28810
28828
globalArrayType = getGlobalType("Array" as __String, /*arity*/ 1, /*reportErrors*/ true);
28811
28829
globalObjectType = getGlobalType("Object" as __String, /*arity*/ 0, /*reportErrors*/ true);
28812
28830
globalFunctionType = getGlobalType("Function" as __String, /*arity*/ 0, /*reportErrors*/ true);
28831
+ globalCallableFunctionType = strictBindCallApply && getGlobalType("CallableFunction" as __String, /*arity*/ 0, /*reportErrors*/ true) || globalFunctionType;
28832
+ globalNewableFunctionType = strictBindCallApply && getGlobalType("NewableFunction" as __String, /*arity*/ 0, /*reportErrors*/ true) || globalFunctionType;
28813
28833
globalStringType = getGlobalType("String" as __String, /*arity*/ 0, /*reportErrors*/ true);
28814
28834
globalNumberType = getGlobalType("Number" as __String, /*arity*/ 0, /*reportErrors*/ true);
28815
28835
globalBooleanType = getGlobalType("Boolean" as __String, /*arity*/ 0, /*reportErrors*/ true);
0 commit comments