@@ -16131,7 +16131,7 @@ namespace ts {
16131
16131
}
16132
16132
16133
16133
function checkDeclarationInitializer(declaration: VariableLikeDeclaration) {
16134
- const type = checkExpressionCached (declaration.initializer);
16134
+ const type = getTypeOfExpression (declaration.initializer, /*cache*/ true );
16135
16135
return getCombinedNodeFlags(declaration) & NodeFlags.Const ||
16136
16136
getCombinedModifierFlags(declaration) & ModifierFlags.Readonly && !isParameterPropertyDeclaration(declaration) ||
16137
16137
isTypeAssertion(declaration.initializer) ? type : getWidenedLiteralType(type);
@@ -16204,10 +16204,12 @@ namespace ts {
16204
16204
16205
16205
// Returns the type of an expression. Unlike checkExpression, this function is simply concerned
16206
16206
// with computing the type and may not fully check all contained sub-expressions for errors.
16207
- function getTypeOfExpression(node: Expression) {
16207
+ // A cache argument of true indicates that if the function performs a full type check, it is ok
16208
+ // to cache the result.
16209
+ function getTypeOfExpression(node: Expression, cache?: boolean) {
16208
16210
// Optimize for the common case of a call to a function with a single non-generic call
16209
16211
// signature where we can just fetch the return type without checking the arguments.
16210
- if (node.kind === SyntaxKind.CallExpression && (<CallExpression>node).expression.kind !== SyntaxKind.SuperKeyword) {
16212
+ if (node.kind === SyntaxKind.CallExpression && (<CallExpression>node).expression.kind !== SyntaxKind.SuperKeyword && !isRequireCall(node, /*checkArgumentIsStringLiteral*/true) ) {
16211
16213
const funcType = checkNonNullExpression((<CallExpression>node).expression);
16212
16214
const signature = getSingleCallSignature(funcType);
16213
16215
if (signature && !signature.typeParameters) {
@@ -16217,7 +16219,7 @@ namespace ts {
16217
16219
// Otherwise simply call checkExpression. Ideally, the entire family of checkXXX functions
16218
16220
// should have a parameter that indicates whether full error checking is required such that
16219
16221
// we can perform the optimizations locally.
16220
- return checkExpression(node);
16222
+ return cache ? checkExpressionCached(node) : checkExpression(node);
16221
16223
}
16222
16224
16223
16225
// Checks an expression and returns its type. The contextualMapper parameter serves two purposes: When
0 commit comments