如何在Typescript中使用CSSNext并避免讨厌的“错误TS2349”的Typescript - 方式

时间:2015-11-12 18:33:38

标签: javascript typescript visual-studio-code tsd postcss

很抱歉,如果这个问题有问题请教。但是每次我想在我的 Typescript 项目中使用外部节点包时,我都会遇到这个讨厌的“错误TS2349”但是有一个 TypeScript定义 :(

这是我的当前设置

node -v v5.0.0
tsc -v message TS6029: Version 1.6.2
OS: 4.1.12-1-ck GNU/Linux Arch 
tsconfig {
    "compilerOptions": {
        "module": "commonjs",
        "target": "ES5",
        "noImplicitAny": false,
        "outDir": "../lib",
        "rootDir": ".",
        "sourceMap": false
    },
}
external node package : "cssnext": "^1.8.4"

我的主要代码

/// <reference path="../definitions/tsd/node/node.d.ts" />
import * as fs from "fs";
import * as cssnext from "cssnext";
let source = "./index.css";
let output = cssnext(
  fs.readFileSync(source, "utf8"),
  {from: source}
);
fs.writeFileSync("dist/index.css", output);

我在寻找什么?

var cssnext = require("cssnext")
var fs = require("fs")

var source = "./index.css"
var output = cssnext(
  fs.readFileSync(source, "utf8"),
  {from: source}
)
fs.writeFileSync("dist/index.css", output)

我得到了什么:(

tsc -p ./src;
src/main.ts(36,14): error TS2349: Cannot invoke an expression whose type lacks a call signature.

** _reference.d.ts有**

declare module "cssnext" {}
declare function cssnext(str: any,ops:Object): string | Object;

真正的问题是

什么是“错误TS2349”用英语说,有了这个背景,我怎么能写一个疯狂的最大 TypeScript定义来修复这个和相关的。 :)

我喜欢Type Script方式,但其他时候:(

**答案**

在下面的帮助下,解决这个问题的代码是:

declare module "cssnext" {
    function cssnext(str: string,ops:Object): string | Object;
    export default cssnext;
} 
import * as cssnext from "cssnext";
let cssnext(str,op)

它可能不是100%cssnext投诉,但它是TSD的起点。

1 个答案:

答案 0 :(得分:2)

这个定义

declare module "cssnext" {}
declare function cssnextLib(str: any,ops:Object): string | Object;

说有一个名为"cssnext"的模块没有成员。这是您在编写import * as cssnextLib from "cssnext";时导入的类型。您在上述定义中编写的cssnextLib函数会被import隐藏(隐藏),因此您无法看到它。

你应该写的是:

declare module "cssnext" {
    function cssnextLib(str: any,ops:Object): string | Object;
    export = cssnextLib;
}