fred jean-germain
fred jean-germain

Reputation: 41

Typescript ambient *.d.ts error TS2304: Cannot find name "*"

I'm trying to write a very simple ambient type in a *.d.ts file. The type (interface) is called "IField" and I'm trying to use it not from import, but as an ambient type. Somehow, I successfully used ambient types in an other projects, but I can't seem able to reproduce it here.

The Intellisense recognizes my "IField" interface, but the Typescript compiler doesn't; instead it returns the following error:

- error TS2304: Cannot find name 'IField'

X:\...\testglobal\node_modules\ts-node\src\index.ts:744
    return new TSError(diagnosticText, diagnosticCodes);
           ^
TSError: ⨯ Unable to compile TypeScript:
src/index.ts:2:17 - error TS2304: Cannot find name 'IField'.

2 const t = {} as IField;
.... 

I've played with the tsconfig and nothing worked so far. Any suggestion on how to solve this, beside importing it? And how is it that the intellisence can recognise my ambient type, but not the compiler?

package.json

{
  "name": "testglobal",
  "version": "1.0.0",
  "description": "",
  "main": "src/index.ts",
  "scripts": {
    "start": "nodemon src/index.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "Fred Jean-Germain",
  "license": "ISC",
  "dependencies": {
    "@types/node": "^16.11.1",
    "nodemon": "^2.0.13",
    "typescript": "^4.4.4"
  }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es6", 
    "module": "commonjs", 
    "esModuleInterop": true, 
    "forceConsistentCasingInFileNames": true, 
    "strict": true, 
  }, 
  "paths": {
    "*": ["./*.d.ts"]
  },
    "exclude": [
        "node_modules"
    ]
}

types.d.ts

declare interface IField {
  name:string
}

src/index.ts

const t = {name:"asdsa"} as IField; 
export function Test() {
  console.log('test', t.name); 
}
Test(); 

Upvotes: 4

Views: 3130

Answers (1)

Denis Zhbankov
Denis Zhbankov

Reputation: 1179

If you use ts-node (like I did), try to add a --files option as mentioned in this GitHub comment: https://github.com/TypeStrong/ts-node/issues/1066#issuecomment-642800652.

Upvotes: 2

Related Questions