简体   繁体   English

本地JS文件的打字稿声明文件

[英]Typescript Declaration Files for Local JS Files

I am trying add typings for our Javascript files at work while we are in the process of converting to Typescript. 我正在尝试在我们正在转换为Typescript的过程中为我们的Javascript文件添加打字。 However, I can not get the declaration files to be recognized. 但是,我无法识别声明文件。

Here is my file structure 这是我的文件结构

  • js JS
    • Foo.js Foo.js
  • typings 分型
    • Foo
      • index.d.ts index.d.ts
  • index.ts index.ts
  • package.json 的package.json
  • tsconfig.json tsconfig.json

Foo.js Foo.js

module.exports = function Foo() {
   return 'Bar';
 };

index.d.ts index.d.ts

export = Foo;

declare function Foo(): string;

index.ts index.ts

import Foo = require('./js/Foo')

console.log(Foo());

tsconfig.json tsconfig.json

{
  "compilerOptions": {
    "typeRoots": ["./typings"],
    "target": "es5",
    "strict": true,
    "baseUrl": "./",
    "paths": {
      "*": ["typings/*"]
    }
  }
}

package.json 的package.json

{
  "name": "fail",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "scripts": {
    "tsc": "tsc"
  },
  "author": "",
  "license": "MIT",
  "dependencies": {
    "typescript": "^3.1.4"
  }
}

Here is repo to reproduce my problem 这是repo来重现我的问题

https://github.com/erisman20/typings_help https://github.com/erisman20/typings_help

Edit: Here is there error I get 编辑:这是我得到的错误

error TS7016: Could not find a declaration file for module './js/Foo.js'. '....../js/Foo.js' implicitly has an 'any' type.

The only ways to provide declarations for a relative import such as './js/Foo' are to actually have the declaration file at the import path (plus .d.ts or /index.d.ts ) or to have it virtually at the import path based on your rootDirs option. 为相对导入提供声明的唯一方法,例如'./js/Foo' ,实际上是在导入路径上有声明文件(加上.d.ts/index.d.ts ),或者实际上是在基于rootDirs选项的导入路径。 Neither typeRoots nor baseUrl / paths comes into play in this scenario: baseUrl / paths only affects module resolution for "non-relative" paths, and typeRoots can be used to get TypeScript to load files but does not affect the relationship between import paths and files at all. 无论typeRoots也没有baseUrl / paths进场在这种情况下: baseUrl / paths只会影响模块的分辨率为“非亲属”路径, typeRoots可以用来获得打字稿加载文件,但不影响输入路径和文件之间的关系一点都不

The simplest solution would be to put a Foo.d.ts file in the same directory as Foo.js . 最简单的解决办法是把一个Foo.d.ts文件在同一目录Foo.js If you want to keep the typings in a separate directory, then you can add "rootDirs": ["js", "typings"] to tsconfig.json . 如果要将"rootDirs": ["js", "typings"]保存在单独的目录中,则可以将"rootDirs": ["js", "typings"] tsconfig.json "rootDirs": ["js", "typings"]tsconfig.json That change is enough to make your example work for me, though I find it confusing to have corresponding Foo.js and Foo/index.d.ts files and would encourage you to be consistent in your use of subdirectories, ie, either switch to Foo/index.js on the JavaScript side or switch to Foo.d.ts on the TypeScript side. 这个改变足以让你的例子适合我,虽然我发现有相应的Foo.jsFoo/index.d.ts文件令人困惑,并鼓励你在使用子目录时保持一致,即切换到JavaScript端的Foo/index.js或者在TypeScript端切换到Foo.d.ts

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM