-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Regression: Not-null assertion causes implicit any #19577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi @mhegazy, is the issue open to take and, can someone guide me on this? Thanks. |
No hypotheses jump immediately to my mind. So, I would:
Report back with the stack and error location if things are still confusing. Anything to do with control flow can be complex and surprising. |
Hi @sandersn . Thanks for the steps.
And the same errors after compiling with local build. Now, as you mentioned in point 5, I should debug the |
@HarshKhatore Yeah, you'll need to search for the error message [1] you're getting and put a breakpoint there. You'll spend forever getting to the actual error otherwise. [1] The messages are mangled like so: |
@sandersn Okay, will try to do that and get back to you :) |
Hi @sandersn, can you help with this? |
@weswigham is our build expert these days. Wesley, any ideas? Here are some things to check in the meantime.
|
Hm. What |
Hi @weswigham . Node version is: |
I meant how did you install the prerequisites in our |
simply: |
Yup - I mostly just wanted to check if you were using a different package manager. |
Hi @weswigham, any update on this? |
Is anyone still on it? Can I give it a shot as a first issue? |
@Bremys give it a shot! Looks like the last attempt was almost 2 years ago. My tips from then still apply except that source maps have been pretty reliable for me lately. |
@sandersn I believe I have managed to find the bug and fix it, but my understanding of the problem and its solution is pretty shallow so I am looking for some affirmation and some technical help. // We only look for uninitialized variables in strict null checking mode, and only when we can analyze
// the entire control flow graph from the variable's declaration (i.e. when the flow container and
// declaration container are the same).
const assumeInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isBindingElement(declaration) ||
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void)) !== 0 ||
isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) ||
node.parent.kind === SyntaxKind.NonNullExpression ||
declaration.kind === SyntaxKind.VariableDeclaration && (<VariableDeclaration>declaration).exclamationToken ||
declaration.flags & NodeFlags.Ambient;
const initialType = assumeInitialized ? (isParameter ? removeOptionalityFromDeclaredType(type, declaration as VariableLikeDeclaration) : type) :
type === autoType || type === autoArrayType ? undefinedType :
getOptionalType(type);
const flowType = getFlowTypeOfReference(node, type, initialType, flowContainer, !assumeInitialized);
// A variable is considered uninitialized when it is possible to analyze the entire control flow graph
// from declaration to use, and when the variable's declared type doesn't include undefined but the
// control flow based type does include undefined.
if (!isEvolvingArrayOperationTarget(node) && (type === autoType || type === autoArrayType)) {
if (flowType === autoType || flowType === autoArrayType) {
if (noImplicitAny) {
/* ERROR thrown here */
error(getNameOfDeclaration(declaration), Diagnostics.Variable_0_implicitly_has_type_1_in_some_locations_where_its_type_cannot_be_determined, symbolToString(symbol), typeToString(flowType));
error(node, Diagnostics.Variable_0_implicitly_has_an_1_type, symbolToString(symbol), typeToString(flowType));
}
return convertAutoToAny(flowType);
}
} The two scenarios (with and without bang operator) first differ in their // We only look for uninitialized variables in strict null checking mode, and only when we can analyze
// the entire control flow graph from the variable's declaration (i.e. when the flow container and
// declaration container are the same).
const isInitialized = isParameter || isAlias || isOuterVariable || isSpreadDestructuringAssignmentTarget || isModuleExports || isBindingElement(declaration) ||
type !== autoType && type !== autoArrayType && (!strictNullChecks || (type.flags & (TypeFlags.AnyOrUnknown | TypeFlags.Void)) !== 0 ||
isInTypeQuery(node) || node.parent.kind === SyntaxKind.ExportSpecifier) ||
declaration.kind === SyntaxKind.VariableDeclaration && (<VariableDeclaration>declaration).exclamationToken ||
declaration.flags & NodeFlags.Ambient;
const assumeInitialized = isInitialized || node.parent.kind === SyntaxKind.NonNullExpression;
const initialType = isInitialized? (isParameter ? removeOptionalityFromDeclaredType(type, declaration as VariableLikeDeclaration) : type) :
type === autoType || type === autoArrayType ? undefinedType :
getOptionalType(type); That is, split My questions are so:
|
A couple of additional notes:
|
Thanks for the reply, I'll create the PR and move the discussion there. |
The type checker is really complex, is there any public resources to learn about it? @sandersn |
This is a very old issue I don't get the same error anymore or at least this works now what would be the solution? function testInitializer(numbers: number[]) {
let last; // any
for (const n of numbers) {
if (n % 2) {
return n;
}
last = n; // assigning 'number' to 'any' works of course
}
return last; // still 'any' so whats the issue now?
} |
@aminpaks I still see the error in the Typescript playground with the 4.1.3 (current latest version). Your code snippet is missing the non-null assertion ( |
@eric-hu ya I see. I'll see what I can do. |
TypeScript Version: 2.7.0-dev.20171029
Code
Workaround:
Initialise the "tracking" variable with undefined.
Expected behavior:
Should compile successfully with and without a bang.
The text was updated successfully, but these errors were encountered: