标签: declaration-files

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

我正在尝试在转换为Typescript的过程中为我们的Javascript文件添加类型。但是,我无法识别声明文件。

这是我的文件结构

  • js
    • Foo.js
  • 打字
    • oo
      • 索引
  • 索引
  • package.json
  • tsconfig.json

Foo.js

module.exports = function Foo() {
   return 'Bar';
 };
Run Code Online (Sandbox Code Playgroud)

索引

export = Foo;

declare function Foo(): string;
Run Code Online (Sandbox Code Playgroud)

索引

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

console.log(Foo());
Run Code Online (Sandbox Code Playgroud)

tsconfig.json

{
  "compilerOptions": {
    "typeRoots": ["./typings"],
    "target": "es5",
    "strict": true,
    "baseUrl": "./",
    "paths": {
      "*": ["typings/*"]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

package.json

{
  "name": "fail",
  "version": "1.0.0",
  "description": "",
  "main": "index.ts",
  "scripts": {
    "tsc": "tsc"
  },
  "author": "",
  "license": "MIT",
  "dependencies": {
    "typescript": "^3.1.4"
  }
}
Run Code Online (Sandbox Code Playgroud)

这是重现我的问题的回购

https://github.com/erisman20/typings_help

编辑:这是我得到的错误 …

javascript declaration type-declaration typescript declaration-files

5
推荐指数
1
解决办法
3015
查看次数

如何安装与旧版TypeScript兼容的@types?

我的项目使用像@types/express和的包@types/body-parser.最近这些.d.ts文件被更新为使用通用默认值,但这意味着这些声明文件现在需要TypeScript 2.3或更高版本.

但是,我的项目仍然使用旧版本的TypeScript - TypeScript 2.2.如何在不升级的情况下使用npm确保我的项目仍能理解这些定义文件?

types npm typescript definitelytyped declaration-files

2
推荐指数
1
解决办法
277
查看次数