MCPcopy Index your code
hub / github.com/nodejs/node / getTypeForVariableLikeDeclaration

Function getTypeForVariableLikeDeclaration

test/fixtures/snapshot/typescript.js:55946–56057  ·  view source on GitHub ↗
(declaration, includeOptionality, checkMode)

Source from the content-addressed store, hash-verified

55944 }
55945 // Return the inferred type for a variable, parameter, or property declaration
55946 function getTypeForVariableLikeDeclaration(declaration, includeOptionality, checkMode) {
55947 // A variable declared in a for..in statement is of type string, or of type keyof T when the
55948 // right hand expression is of a type parameter type.
55949 if (ts.isVariableDeclaration(declaration) && declaration.parent.parent.kind === 243 /* SyntaxKind.ForInStatement */) {
55950 var indexType = getIndexType(getNonNullableTypeIfNeeded(checkExpression(declaration.parent.parent.expression, /*checkMode*/ checkMode)));
55951 return indexType.flags & (262144 /* TypeFlags.TypeParameter */ | 4194304 /* TypeFlags.Index */) ? getExtractStringType(indexType) : stringType;
55952 }
55953 if (ts.isVariableDeclaration(declaration) && declaration.parent.parent.kind === 244 /* SyntaxKind.ForOfStatement */) {
55954 // checkRightHandSideOfForOf will return undefined if the for-of expression type was
55955 // missing properties/signatures required to get its iteratedType (like
55956 // [Symbol.iterator] or next). This may be because we accessed properties from anyType,
55957 // or it may have led to an error inside getElementTypeOfIterable.
55958 var forOfStatement = declaration.parent.parent;
55959 return checkRightHandSideOfForOf(forOfStatement) || anyType;
55960 }
55961 if (ts.isBindingPattern(declaration.parent)) {
55962 return getTypeForBindingElement(declaration);
55963 }
55964 var isProperty = ts.isPropertyDeclaration(declaration) || ts.isPropertySignature(declaration);
55965 var isOptional = includeOptionality && (isProperty && !!declaration.questionToken ||
55966 ts.isParameter(declaration) && (!!declaration.questionToken || isJSDocOptionalParameter(declaration)) ||
55967 isOptionalJSDocPropertyLikeTag(declaration));
55968 // Use type from type annotation if one is present
55969 var declaredType = tryGetTypeFromEffectiveTypeNode(declaration);
55970 if (declaredType) {
55971 return addOptionality(declaredType, isProperty, isOptional);
55972 }
55973 if ((noImplicitAny || ts.isInJSFile(declaration)) &&
55974 ts.isVariableDeclaration(declaration) && !ts.isBindingPattern(declaration.name) &&
55975 !(ts.getCombinedModifierFlags(declaration) & 1 /* ModifierFlags.Export */) && !(declaration.flags & 16777216 /* NodeFlags.Ambient */)) {
55976 // If --noImplicitAny is on or the declaration is in a Javascript file,
55977 // use control flow tracked 'any' type for non-ambient, non-exported var or let variables with no
55978 // initializer or a 'null' or 'undefined' initializer.
55979 if (!(ts.getCombinedNodeFlags(declaration) & 2 /* NodeFlags.Const */) && (!declaration.initializer || isNullOrUndefined(declaration.initializer))) {
55980 return autoType;
55981 }
55982 // Use control flow tracked 'any[]' type for non-ambient, non-exported variables with an empty array
55983 // literal initializer.
55984 if (declaration.initializer && isEmptyArrayLiteral(declaration.initializer)) {
55985 return autoArrayType;
55986 }
55987 }
55988 if (ts.isParameter(declaration)) {
55989 var func = declaration.parent;
55990 // For a parameter of a set accessor, use the type of the get accessor if one is present
55991 if (func.kind === 173 /* SyntaxKind.SetAccessor */ && hasBindableName(func)) {
55992 var getter = ts.getDeclarationOfKind(getSymbolOfNode(declaration.parent), 172 /* SyntaxKind.GetAccessor */);
55993 if (getter) {
55994 var getterSignature = getSignatureFromDeclaration(getter);
55995 var thisParameter = getAccessorThisParameter(func);
55996 if (thisParameter && declaration === thisParameter) {
55997 // Use the type from the *getter*
55998 ts.Debug.assert(!thisParameter.type);
55999 return getTypeOfSymbol(getterSignature.thisParameter);
56000 }
56001 return getReturnTypeOfSignature(getterSignature);
56002 }
56003 }

Callers 4

checkTryStatementFunction · 0.85
getTypeOfNodeFunction · 0.85

Calls 15

getIndexTypeFunction · 0.85
getExtractStringTypeFunction · 0.85
getTypeForBindingElementFunction · 0.85
isJSDocOptionalParameterFunction · 0.85
addOptionalityFunction · 0.85
isNullOrUndefinedFunction · 0.85
isEmptyArrayLiteralFunction · 0.85
hasBindableNameFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…