π Search Terms
require shadowing javascript import scope local
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?esModuleInterop=false&jsx=0&module=0&filetype=js&q=422#code/GYVwdgxgLglg9mABAUwB4EMC2AHANsxACgEpEBvAKEWsQHoAqexAAW3QCctyBnKdmMAHMAvojBYC9WlRqhIsBInbIAjiBjLC4zMlJklyKCHZIAciEwAjZOy0TSwmdQgJeibnB3mrNxAF4DNQ1kQgByDy8JUOIAbgphIA
π» Code
function () {
/** @param {string} name */
function require(name) { return Number(name) }
const someNumber = require('someName');
}
π Actual behavior
someNumber is an import.
- It raises a type import error:
Cannot find module 'someName' or its corresponding type declarations.
π Expected behavior
someNumber has type number.
- No error is raised.
Additional information about the issue
This is only the case when using type inference with JavaScript files. With TypeScript it works as expected (playground):
function () {
function require(name: string) { return Number(name) }
const someNumber = require('someName');
}
If the type of someNumber is explicitely written, it also works as expected (playground):
function () {
/** @param {string} name */
function require(name) { return Number(name) }
/** @type {number} */
const someNumber = require('someName');
}
π Search Terms
require shadowing javascript import scope local
π Version & Regression Information
requiremodule resolution was introduced in 4.1.0-dev.20200818 with Alias for commonjs require in JSΒ #39770).β― Playground Link
https://www.typescriptlang.org/play/?esModuleInterop=false&jsx=0&module=0&filetype=js&q=422#code/GYVwdgxgLglg9mABAUwB4EMC2AHANsxACgEpEBvAKEWsQHoAqexAAW3QCctyBnKdmMAHMAvojBYC9WlRqhIsBInbIAjiBjLC4zMlJklyKCHZIAciEwAjZOy0TSwmdQgJeibnB3mrNxAF4DNQ1kQgByDy8JUOIAbgphIA
π» Code
π Actual behavior
someNumberis an import.π Expected behavior
someNumberhas typenumber.Additional information about the issue
This is only the case when using type inference with JavaScript files. With TypeScript it works as expected (playground):
If the type of
someNumberis explicitely written, it also works as expected (playground):