microsoft / microsoft/TypeScript

tsc gets confused after assigning to module.exports - Property 'filename' does not exist on type

Open
#30,088 1 comment 1 reaction 1 assignee View on GitHub

@sandersn is already working on this.

Since Feb 27, 2019.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.4.0-dev.20190223

Search Terms:
module.filename
require.main === module
Code

// A *self-contained* demonstration of the problem follows...
// Test this by running `tsc` on the command-line, rather than through another build tool such as Gulp, Webpack, etc.
module.exports = function(){ 
  return module.filename;
} 

Expected behavior:
Should not emit any error
Actual behavior:

D:\test>node_modules\.bin\tsc -v
Version 3.4.0-dev.20190223

D:\test>node_modules\.bin\tsc
test.js:2:17 - error TS2339: Property 'filename' does not exist on type '{ "D:/test/test": () => any; }'.

2   return module.filename;
                  ~~~~~~~~


Found 1 error.

Playground Link:
tsconfig.json:

{
  "compilerOptions": {
    "noEmit": true,
    "checkJs": true,
    "allowJs": true,
    "resolveJsonModule": true, 
    "module": "commonjs",
    "lib": ["es2018"],
    "target": "es2018",
    "baseUrl": ".",
    "paths": {
      "@/*": [
        "../*"
      ],
      "#/*": [
        "./*"
      ]
    },
    "types": [
      "node"
    ],
  },
  "exclude": [
    "node_modules",
    "public"
  ]
}

Related Issues:
https://github.com/DefinitelyTyped/DefinitelyTyped/issues/33357

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.