0

关于如何让自定义类型在anuglar2 快速入门项目中工作的一些事情。

我以outlayerhttps://github.com/metafizzy/outlayer为例。

我已配置systemjs为加载依赖项。它是可用的,import * as outlayer from "outlayer";但我仍然得到编译错误Cannot find module 'outlayer'.

为了解决这个问题,outlayer.d.ts直接在node_modules/outlayer目录中添加了一个类型文件并更新了package.json包含"typings": "./outlayer.d.ts",.

//outlayer.d.ts

declare module Outlayer {

  function create(str:string):any;
}

export = Outlayer;

出于显而易见的原因,将此文件直接添加到 node_module 并不理想。有什么更好的方法来处理这个而不必分叉库?更好的是,我怎样才能在应用程序级别进行简单/快速的打字,这样我就不必担心马上写出整个事情?例如,如果我现在尝试使用该data功能,我会看到Property 'data' does not exist on type 'typeof Outlayer'

总之,只需要能够编写一个自定义的应用程序级别的类型,它没有所有的好东西,但也不会出现所有的错误。一种临时打字。

谢谢!

4

1 回答 1

1

这是您需要做的:

  1. 在某处创建文件(例如custom-typings/outlayer.d.ts
  2. 使其成为脚本文件(全局类型):

    declare module 'outlayer' {
      function create(str: string): any;
    }
    
  3. 安装typings install file:custom-typings/outlayer.d.ts --global

看看有没有帮助。

编辑:更新了上面的第 3 步以添加--global

于 2016-07-06T17:51:43.147 回答