Skip to content

Improve control flow analysis of type assertions#26679

Merged
ahejlsberg merged 3 commits into
masterfrom
improveGetTypeOfExpression
Aug 28, 2018
Merged

Improve control flow analysis of type assertions#26679
ahejlsberg merged 3 commits into
masterfrom
improveGetTypeOfExpression

Conversation

@ahejlsberg
Copy link
Copy Markdown
Member

Fixes #26655.

Comment thread src/compiler/checker.ts
* to cache the result.
*/
function getTypeOfExpression(node: Expression, cache?: boolean) {
const expr = skipParentheses(node);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we can do this quite like this in this codepath - this means this fast path will cause us to skip jsdoc type assertions in JS, which look like /* @type {Type} */(expression).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same example as repro, but in JS:

// @ts-check
/**
* @typedef {{message: {id: string}}} DataShape
*/

/**
* @param {string | number} id
* @return {any}
*/
function getObject(id) {
  return {} as any
}

;(() => {
  /* @type {string | number} */
  let id = 'a'
  while (1) {
    const data = /* @type {DataShape} */(getObject(id))
    const message = data.message
    id = message.id
  }
})()

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can do it this way, it just means that we won't bail out early during control flow analysis. It's simply a matter of how many cases we feel are worthwhile handling in getTypeOfExpression. In a TypeScript file it matters because you'll get an error with --noImplicitAny. In a JavaScript file it just means another any, which are a dime a dozen already, so to speak. So, I think it is fine.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noImplicitAny still works in JS files with checkJs, so the bug does still exist in JS then, it just has a smaller audience.

@ahejlsberg ahejlsberg merged commit ac0d5da into master Aug 28, 2018
@ahejlsberg ahejlsberg deleted the improveGetTypeOfExpression branch August 28, 2018 01:30
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants