Javascript 如何在Typescript中使用CSSNext并避免令人讨厌的;错误TS2349“;打字方式
对不起,如果这个问题已经问了又问。但我一直在点击这个讨厌的“错误TS2349”,每次我想在我的Typescript项目中使用外部节点包时,什么还没有Typescript定义呢( 这是我目前的设置Javascript 如何在Typescript中使用CSSNext并避免令人讨厌的;错误TS2349“;打字方式,javascript,typescript,visual-studio-code,tsd,postcss,Javascript,Typescript,Visual Studio Code,Tsd,Postcss,对不起,如果这个问题已经问了又问。但我一直在点击这个讨厌的“错误TS2349”,每次我想在我的Typescript项目中使用外部节点包时,什么还没有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",
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);
我得到的:(
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”在英语中的意思是什么?我如何编写mad maxTypeScript定义来修复此错误和相关错误:
我喜欢打字脚本的方式,但其他时候:(
**答复**
在下面的帮助下,解决此问题的代码是:
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的起点。此定义
declare module "cssnext" {}
declare function cssnextLib(str: any,ops:Object): string | Object;
表示有一个名为“cssnext”
的模块没有成员。这是当您从“cssnext”将import*作为cssnextLib写入时导入的类型;
。您在上述定义中编写的cssnextLib
函数被import
隐藏,因此您无法看到它
你应该写的是:
declare module "cssnext" {
function cssnextLib(str: any,ops:Object): string | Object;
export = cssnextLib;
}
嘿,Ryan,我认为与问题中所描述的不同,
cssnext
库中的函数是,而不是cssnextLib
。名称cssnextLib
在模块声明之外不可见哦,是的,我的错。我被搞糊涂了,因为import*是“cssnext”中的cssnextLib
。我没有注意到导出=
。我认为它应该使用默认导入,然后从“cssnext”导入cssnextLib;
?@ryan cavanaugh这不是我的错,不是David,我cssnextLib不是cssnext,因此不应该在定义文件中:),但您的解决方案打开了一个新的错误TS2497:Module'cssnext''解析为非模块实体,无法使用此构造导入。如果现在应用于上面:(必须在“cssnext”声明中添加命名空间cssnextLib{}
。
declare module "cssnext" {
function cssnextLib(str: any,ops:Object): string | Object;
export = cssnextLib;
}