1

Here is the repo for this question and if you want you can clone that repo, compile using TypeScript, and run it in node to see that there is a runtime error, but no compile time error.

To cut to the chase: why do I not get a compile time error by TypeScript in file1 when I try to run a method of an object imported from another module file2, given that the object has no methods?

This is the code, basically:

To set up the project:

mkdir test
cd test
npm init --yes
yarn add typescript
touch tsconfig.json
touch index.ts
touch jsModule.js

Here is tsconfig.json:

{
  "compilerOptions": {
    "allowJs": false,
    "allowSyntheticDefaultImports": true,
    "experimentalDecorators": true,
    "module": "es2015",
    "moduleResolution": "node",
    "noImplicitAny": false,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "sourceMap": true,
    "target": "es2015",
    "skipLibCheck": true,
    "strict": true,
    "resolveJsonModule": true
  },
  "exclude": ["node_modules"]

}

Here are the two files I created:

// jsModule.js

export const MyModuleObject = {};

// index.ts
import { MyModuleObject } from './jsModule';
console.log(MyModuleObject.method());

Problem: Typescript does not say anything about me calling MyModuleObject.method()

evianpring
  • 3,316
  • 1
  • 25
  • 54
  • I am not sure but `allowSyntheticDefaultImports` option might be issue as it effects type checking. You might want to take a look at the [this](https://github.com/Microsoft/TypeScript/pull/26866/commits/aaa723e2d2ee393331ac9d8eba29d66b80f415fe) and [this](https://stackoverflow.com/questions/52576203/do-i-ever-need-explicit-allowsyntheticdefaultimports-if-esmoduleinterop-is-true). – Mahesh Apr 26 '19 at 13:24
  • why is `jsModule` a javascript file? Can you convert `jsModule.js` to typescript? – shusson Apr 26 '19 at 13:57
  • This question came up when I was trying to understand the typescript `declare` keyword. I wanted to have a TS file that imports an untyped module (as I'm doing in this example), and I expected TS to show me errors (which I'm not seeing and is the reason for this question). I would then remedy those errors by using the `declare` keyword to declare whatever is being exported from the untyped JS file. Not sure if my line of reasoning is correct, but this was my intention to understand `declare`. – evianpring Apr 26 '19 at 14:00
  • By default the compiler will not typecheck js files. You can enable it with the `checkJs` in combination with `allowJs` compiler options https://github.com/Microsoft/TypeScript/wiki/Type-Checking-JavaScript-Files – shusson Apr 26 '19 at 14:06
  • Please see this too, my next question, about this: https://stackoverflow.com/questions/55869440/what-is-the-simplest-possible-example-of-where-declare-keyword-is-useful-in-ty – evianpring Apr 26 '19 at 14:11

1 Answers1

3

"noImplicitAny": false in tsconfig.json causes your problem.

It simply means "allow implicit any". Since TS knows nothing about what is exported from jsModule.js, you effectively allow TS to assign any as the type of that module. MyModuleObject.method() becomes any.any(), which is fine.

Set "noImplicitAny": true, then TS will complain.

hackape
  • 18,643
  • 2
  • 29
  • 57