我正在尝试在C#中编写一个函数,该函数接收包含typescript代码的字符串并返回包含JavaScript代码的字符串.这有库函数吗?
运行 tsc 时,我得到许多TS2688: Cannot find type definition file for 'someLibrary'
这些库来自node_modules. 我尝试在 tsconfig 中排除node_modules和skipLibCheck,但它们都不适合我。知道为什么会发生这种情况吗?
这是我的 tsconfig.json
{
"ts-node": {
"files": true,
"emit": true,
"compilerHost": true
},
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "es2016",
"sourceMap": true,
"typeRoots" : ["./node_modules","./node_modules/@types"],
"jsx": "react",
"allowSyntheticDefaultImports": true,
"skipLibCheck": true,
"esModuleInterop": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "commonJS",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
},
"exclude": [
"node_modules"
]
}
Run Code Online (Sandbox Code Playgroud) {
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试将angular2/beta8应用程序升级到RC1,而我正在根据Quickstart指南进行基本重组.
我将其tsconfig.json复制到我的项目目录中.我想我已准备好其他一切,但是当我运行时tsc,我在node_modules文件夹中的文件中会遇到各种错误.为什么它首先在那里寻找?
我正在尝试使用该--strict选项,tsc但我遇到了以下"奇怪"的情况,我似乎并不理解.
如果我写:
function testStrict(input: {query?: {[prop: string]: string}}) {
if (input.query) {
Object.keys(input.query).forEach(key => {
input.query[key];
})
}
return input;
}
Run Code Online (Sandbox Code Playgroud)
编译器抱怨:
test.ts(5,9):错误TS2532:对象可能是"未定义的".
(违规行是input.query[key];)
我不明白的是,我已经在函数的第一行用if guard检查了undefined if (input.query),为什么编译器认为它可能是未定义的?
我通过在对象访问之前添加另一个相同的防护来修复此问题,例如:
function testStrict(input: {query?: {[prop: string]: string}}) {
if (input.query) {
Object.keys(input.query).forEach(key => {
if (input.query) {
input.query[key];
}
})
}
return input;
}
Run Code Online (Sandbox Code Playgroud)
但我不明白为什么需要另一条相同的线.
我全局安装了typescript(npm install typescript -g)
然后创建了一个文件夹,运行 npm --init,然后npm intall typescript --save-dev- 它安装了typescript@2.1.4
在文件夹中,我创建了'helloworld.ts`
var msg = 'Hello World';
console.log (msg);
Run Code Online (Sandbox Code Playgroud)
使用文件选项运行tsc命令 - tsc helloworld.ts并将其编译为helloworld.js.
接下来,我想使用tsconfig.json,所以我运行tsc --init- 这不起作用,说Unknown option 'init'
我说没问题,让我尝试手动添加tsconfig.json并将其添加到文件夹root中,如下所示:
{
"compilerOptions": {
"target": "es5"
},
"files": [
"helloworld.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
我tsc在命令提示符下运行,但它不起作用,并输出如何使用tsc的语法,示例和选项Syntax: tsc [options] [file] ...
怎么了?
where tsc 给出以下:
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\tsc.exe
C:\Program Files (x86)\Microsoft SDKs\TypeScript\1.0\tsc.js
C:\Users\Kap\AppData\Roaming\npm\tsc
C:\Users\Kap\AppData\Roaming\npm\tsc.cmd
Run Code Online (Sandbox Code Playgroud) 问题:不使用any,我的onMouseMove函数的正确类型是什么?
export class Main {
private dTimer: number;
constructor() {
this.init();
}
private init() {
this.mouseHandlers();
}
private mouseHandlers() {
document.addEventListener('mousemove', this.onMouseMove)
}
private onMouseMove: EventListener = (event: MouseEvent) => {
clearTimeout(this.dTimer);
this.dTimer = setTimeout(() => {
console.log(event.pageX, event.pageY)
}, 500);
}
}
Run Code Online (Sandbox Code Playgroud)
Typescript 抱怨我的类型,我不知道如何在不使用任何类型的情况下让它开心。
main.ts(38,3): error TS2322: Type '(event: MouseEvent) => void' is not assignable to type 'EventListener'.
Types of parameters 'event' and 'evt' are incompatible.
Type 'Event' is not assignable to type 'MouseEvent'. …Run Code Online (Sandbox Code Playgroud) 我想在我的 typescript Nodejs 项目中有顶级的等待。
我的 tsconfig 过去看起来像这样:
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
#### other stuff which I think is not relevant removed ####
Run Code Online (Sandbox Code Playgroud)
我现在把它切换到
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"lib": [
"dom",
"es6",
"es2017",
"esnext.asynciterable"
],
"skipLibCheck": true,
"sourceMap": true,
"outDir": "./dist",
"moduleResolution": "node",
#### other stuff which I think is not relevant removed ####
Run Code Online (Sandbox Code Playgroud)
我还添加到"type": "module"了我的 package.json 中。事实上我现在有能力做顶级的等待 …
我正在运行typescript构建并在node_modules中获取错误.为什么不忽略这个文件夹?我在tsconfig.json的exclude部分中有它.真正奇怪的是我有另一个项目,我已经完成了文件比较,即使gulpfile.js,tsconfig.json和node_modules文件夹相同,它也不会抛出这些错误.我还能检查什么?
错误:
c:/Dev/streak-maker/node_modules/angular2/src/core/change_detection/parser/locals.d.ts(3,14): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/change_detection/parser/locals.d.ts(4,42): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/debug/debug_node.d.ts(14,13): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/debug/debug_node.d.ts(24,17): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/debug/debug_node.d.ts(25,17): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/di/provider.d.ts(436,103): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/di/provider.d.ts(436,135): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/render/api.d.ts(13,13): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/core/render/api.d.ts(14,84): error TS2304: Cannot find name 'Map'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(1,25): error TS2304: Cannot find name 'MapConstructor'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(2,25): error TS2304: Cannot find name 'SetConstructor'.
c:/Dev/streak-maker/node_modules/angular2/src/facade/collection.d.ts(4,27): …Run Code Online (Sandbox Code Playgroud) 说我有这种类型:
export interface Opts {
paths?: string | Array<string>,
path?: string | Array<string>
}
Run Code Online (Sandbox Code Playgroud)
我想告诉用户他们必须传递路径或路径,但没有必要通过它们.现在的问题是这个编译:
export const foo = (o: Opts) => {};
foo({});
Run Code Online (Sandbox Code Playgroud)
有谁知道允许2个或更多可选但至少1是TS的必要参数?
我正在构建一个discord.jsDiscord 机器人。现在由于某种原因,discord.js不适用于ESM模块(一个完全独立的问题),所以我的机器人应用程序使用CommonJS模块。现在我的系统上有另一个名为 的项目Lib,它有很多实用函数,我计划在几个不同的项目中使用这些函数,因此我不必重写它们。该Lib项目使用ESM模块。因为我不得不进口Lib来自DiscordBot,我用打字稿动态导入语法。现在,每当我转译我的DiscordBot项目时,动态导入都会被转换成一些丑陋的 javascript 模块代码,而那些丑陋的模块代码最终会使用 require()。由于 require() 无法导入 ESM 模块,我的机器人最终崩溃了。
然而,我试图停止我的 ts 编译器,从我导入的 ts 文件中复制代码,Lib然后将该代码手动粘贴到相应的 JS 文件中(并删除 TS 独有的功能,如类型注释和接口)。然后我运行了我的机器人应用程序,它运行得非常好。但我不想每次都这样做。所以这tsc是编译的问题。我该如何解决?
tsc ×10
typescript ×10
javascript ×2
node.js ×2
angular ×1
c# ×1
compilation ×1
gulp ×1
mouseevent ×1
mousemove ×1
npm ×1
tsconfig ×1
typing ×1