question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

Getting type information for a node causes other nodes to return any for their type

See original GitHub issue

Bug Report

๐Ÿ”Ž Search Terms

Compiler API, unexpected any

๐Ÿ•— Version & Regression Information

  • This changed between versions 4.5.x and 4.6.x

This does not repro on 4.5.5, but repros against v4.6.2

โฏ Playground Link

Playground link with relevant code

๐Ÿ’ป Code

export function getFutureVersion(baseVersion?: string): number[] {
  const toRelease: number[] = [];
  const baseRelease: number[] = [];
  return baseRelease.map((_, index) => {
    const toPart = toRelease[index] ?? 0;
    if (index < 1) {
      return toPart;
    }
    if (index === 1) {
      toPart;
      //  ^? any
      toPart.lol; // Property 'lol' does not exist on type 'number'.(2339)
      //  ^? any
      return toPart + (baseVersion === undefined ? 0 : 1); 
    }
    return 0;
  });
}

๐Ÿ™ Actual behavior

You can see that in this repro intellisense reports the type of toPart as any even though TS also knows that it must be a number.

If I attempt to get the type of toPart:

window.sandbox.createTSProgram().then(program => {
  program.emit()
  const ast = program.getSourceFile('/input.tsx');
  console.log(
    program.getTypeChecker().getTypeAtLocation(
      ast.statements[0].body.statements[2].expression.arguments[0].body.statements[2].thenStatement.statements[0].expression,
    ),
  );
});

// -> {checker: {โ€ฆ}, flags: 1, id: 1, intrinsicName: 'any', objectFlags: 0}

Hazarding a guess intellisense and the ^? playground token both just use checker.getTypeAtLocation(node) to get the type (and checker.typeToString to print it).

If you switch the TS version to 4.5 then youโ€™ll see that the type is correctly printed as number and the above snippet logs:

{checker: {โ€ฆ}, flags: 8, id: 14, intrinsicName: 'number', objectFlags: 0}

๐Ÿ™‚ Expected behavior

The 4.5 behaviour is correct - getTypeAtLocation should return a number type.

Related

https://github.com/typescript-eslint/typescript-eslint/issues/4689

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:10
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

github_iconTop Results From Across the Web

Node properties: type, tag and contents
The tagName property exists only for Element nodes. The nodeName is defined for any Node : for elements it means the same as...
Read more >
Node (JavaFX 8) - Oracle Help Center
Specifies whether this Node and any subnodes should be rendered as part of the scene graph. Field Summary. Fields. Modifier and Type, Field...
Read more >
MATCH - Cypher Manual - Neo4j
Get all nodes โ€‹โ€‹ By just specifying a pattern with a single node and no labels, all nodes in the graph will be...
Read more >
Writing Functions - Node-RED
In general, function nodes should return the message object they were passed ... This makes it easy to write a function that sends...
Read more >
NodeList - Web APIs | MDN
Chrome Edge Fir... NodeList Full support. Chrome1. Toggle history Full support. Edge12. Toggle history Fu... @@iterator Full support. Chrome51. Toggle history Full support. Edge79. Toggle...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found