【问题标题】:How to use CSSNext in Typescript and avoid pesky "error TS2349" the Typescript - way如何在 Typescript 中使用 CSSNext 并避免讨厌的“错误 TS2349”Typescript - 方式
【发布时间】:2016-02-14 05:01:09
【问题描述】:

对不起,如果这个问题有豆问和问。但是我总是遇到这个讨厌的 "error TS2349",每次我想在我的 Typescript 项目中使用 external Node Package 时,怎么办?还有一个 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;

真正的问题是

什么是英文中的“error TS2349”,在这种情况下,我该如何编写 ma​​d max 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 的起点。

【问题讨论】:

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


    【解决方案1】:

    这个定义

    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;
    }
    

    【讨论】:

    • 嘿 Ryan,我认为与问题中描述的不同,cssnext 库中的函数是 cssnext 而不是 cssnextLib
    • 名称cssnextLib在模块声明之外是不可见的
    • 哦,是的,我的错。我因为import * as cssnextLib from "cssnext" 而感到困惑。我没有注意到export =。我认为它应该使用默认导入——import cssnextLib from "cssnext";?
    • @ryan-cavanaugh 这是我的错,不是大卫,我 cssnextLib 不是 cssnext,因此不应该在定义文件中:) 但是你的解决方案打开了一个新错误 error TS2497: Module '" cssnext"' 解析为非模块实体,无法使用此构造导入。 如果现在应用于上面:(
    • 您必须在“cssnext”声明中添加namespace cssnextLib {}
    猜你喜欢
    • 2020-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    相关资源
    最近更新 更多