Skip to content

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

@bradzacher

Description

@bradzacher

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

typescript-eslint/typescript-eslint#4689

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions