Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

why is an interface type not found is a Typescript file

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In case of using command line (cli), you won't be able to give both file option and tsconfig.json file (via --project option) which means if you provide your file main.ts, you must have to provide your typing for replays as well but it seems to be hard to give 2 files at the same time (you could find out more about pattern to include them both)

Anyway I would suggest you to follow up 2 either options:

  • Put things into configuration file using include option: tsconfig.json
{
  "compilerOptions": {
    // ...
  },
  "include": [
    "src/**/*.ts"
  ]
}

Then you just simply run: npx tsc without passing anything else

  • Still passing files via CLI but you should put them into a dir so it's easier to pass together for example:
- src
-- main.ts
-- replays.d.ts

Then you run them all:

npx tsc src/*

PS: keep in mind, the tsconfig has no effect in this case


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...