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
Bug Report
π Search Terms
Compiler API, unexpected any
π Version & Regression Information
This does not repro on 4.5.5, but repros against v4.6.2
β― Playground Link
Playground link with relevant code
π» Code
π Actual behavior
You can see that in this repro intellisense reports the type of
toPartasanyeven though TS also knows that it must be a number.If I attempt to get the type of
toPart:Hazarding a guess intellisense and the
^?playground token both just usechecker.getTypeAtLocation(node)to get the type (andchecker.typeToStringto print it).If you switch the TS version to 4.5 then you'll see that the type is correctly printed as
numberand the above snippet logs:π Expected behavior
The 4.5 behaviour is correct -
getTypeAtLocationshould return anumbertype.Related
typescript-eslint/typescript-eslint#4689