When I run tsc in the 'proj' directory with --traceResolution, I see the module 'foo' is "successfully resolved" but not "found":
======== Module name 'foo' was successfully resolved to 'C:/dev/ts-options/typings/foo/index.d.ts'. ========
main.ts(1,18): error TS2307: Cannot find module 'foo'.
Setting "noResolve": true
in tsconfig.compilerOptions makes the error go away. But I'm supr-confused!
Question 1: What is the difference between "resolution" and "finding"? I thought these were synonyms
Question 2: Why does 'noResolve' affect this behavior? What does noResolve actually do? TS compiler options docs say "Do not add triple-slash references or module import targets to the list of compiled files." but I don't know what that means.
With this directory structure
- /c/dev/ts-options/
- typings/
- foo
- index.d.ts
- foo
- proj this is where I'm running tsc
- tsconfig.json
- main.ts
- typings/
proj/tsconfig.json (problem is fixed if I change noResolve to false):
{
"compilerOptions": {
"baseUrl": ".",
"types": [],
"outDir": "../out",
"paths": {
"foo": [
"../typings/foo"
],
},
"noResolve": true
},
"include": [
"**/*.ts",
],
"exclude": [
"node_modules"
]
}
proj/main.ts:
import foo from "foo"
typings/foo/index.d.ts:
declare function foo(num: number): number;
export default foo;
Update
Two solutions:
- Set noResolve: false
- OR add "../typings" to the "include" section in the tsconfig
My guess is that there is a concept of a "compilation unit" such that:
with
noResolve: false
then the compilation unit can grow dynamically as TS followsimport
s.With
noResolve: true
, then the compilation unit is fixed to something liketsconfig.baseUrl/** + (tsconfig.include - tsconfi.exclude)
.When a file is outside the compilation unit then TS can 'resolve' an import (find out the path to a .d.ts/.ts/.tsx file) but won't 'find' it (actually read the file).
This still doesn't really make sense to me, though. What's the point of 'resolving' to a location one knows one won't be able to 'find'?