Skip to content

tsc complains require() file path outside of rootDir only when await import() is used #38611

Closed

Description

@ychi

TypeScript Version: 4.0.0-dev.20200516

Search Terms: commonjs, esmodule, rootDir

Code
Consider this file:

onst {version: VERSION} = require('../package.json');
console.log(VERSION);

async function foo() {
    let obj = {};
    console.log(obj);
}

export async function bar() {
    console.log('foo');
    await foo();
}

And this file:

const {version: VERSION} = require('../package.json');
console.log(VERSION);

async function foo() {
    let obj = await import('./anotherFile');
    console.log(obj);
}

export async function bar() {
    console.log('foo');
    await foo();
}

Expected behavior:
Running tsc to compile them, they should both succeed or both fail?
Actual behavior:
The first file can be compiled smoothly, while the second was complained about error TS6059 and error TS6307.
Playground Link: https://glitch.com/~aromatic-bittersweet-biplane

Related Issues:

Activity

  1. cspotcode commented on May 17, 2020

    @cspotcode

    I am seeing this bug triggered by a slightly different reason: adding an import() type annotation.

    I have created a minimal reproduction here: https://github.com/TypeStrong/ts-node-repros/tree/ts-38611
    It executed here: https://github.com/TypeStrong/ts-node-repros/runs/682702614

    declare const require: any;
    
    // Bug occurs when an import() is in the file
    const a: typeof import('./other') = null as any
    
    function foo() {
        const a = require('../outside-of-rootdir/foo'); // Semantic error: File ... is not under 'rootDir' ...
        const {other}: {other: string} = require('./other'); // No error here, even though is semantically invalid, because `other` is of type `number`.
        
        // It appears as though the resolver is trying to locate the targets of require() calls, but the typechecker does not care.
    }
    
  2. sheetalkamat commented on Jul 15, 2020

    @sheetalkamat
    Member

    ychi your repro link seems to be not working, Can you please share the repro again. Thanks

  3. added
    Needs More InfoThe issue still hasn't been fully clarified
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Jul 15, 2020
  4. sheetalkamat commented on Jul 15, 2020

    @sheetalkamat
    Member

    Actually Andrew Bradley (@cspotcode) repro seems to be working.. Thanks.

  5. added
    Needs InvestigationThis issue needs a team member to investigate its status.
    BugA bug in TypeScript
    and removed
    Needs More InfoThe issue still hasn't been fully clarified
    on Jul 15, 2020
  6. added a commit that references this issue on Jul 15, 2020
    1f95af0
  7. added
    Fix AvailableA PR has been opened for this issue
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Jul 15, 2020
  8. added a commit that references this issue on Jul 15, 2020
    37e6e27
  9. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

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