Edit: I am updating this question as originally I misunderstood what *.d.ts files are used for. The problem is either one of "type space" vs "value space" or that the compiler can't find the interface definition.
I created an empty TypeScript project:
npm i typescript --save-dev
npx tsc --init
tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
},
"include": [
"src/**/*.ts",
"backend_types/**/*.ts"
]
}
I believe the default import strategy is Node?
I created src/backend_types/relays.ts
:
export interface Relay {
Name: string;
}
export function newRelay(): Relay {
const relay: Relay = {
Name: '',
};
return relay;
}
And created srcmain.ts
:
import { Relay, newRelay } from 'relays'
let relay: Relay = newRelay()
console.log(relay)
Now, when I compile the code, tsc can not find the Relay type:
> npx tsc
src/main.ts:1:33 - error TS2307: Cannot find module 'relays' or its corresponding type declarations.
1 import { Relay, newRelay } from 'relays';
~~~~~~~~
Indeed, if I look in the javascript generated from relays.ts
the function exists but the Relay type does not. This makes sense as "types" do not exist in javascript, only in typescript, and an interface is a type. I am apparently mixing up "type space" and "value space".
My question is this: I can't even create a "value" instance of the interface type Relay in main.ts as the compiler can't seem to find the type definition. What do I need to change in tsconfig.json to get the compiler to find that interface definition? If I use the --traceResolution
switch every line in the output shows a path including node_modules
- what if you want to put type definitions elsewhere?
question from:
https://stackoverflow.com/questions/65848179/why-is-an-interface-type-not-found-is-a-typescript-file 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…