Skip to content

JavaScript file: Shadowing require still behaves like the global oneΒ #61528

Closed as not planned
@Derugon

Description

@Derugon

πŸ”Ž 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

  1. someNumber is an import.
  2. It raises a type import error:
Cannot find module 'someName' or its corresponding type declarations.

πŸ™‚ Expected behavior

  1. someNumber has type number.
  2. 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');
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions