我刚刚将TypeScript安装为Node.js软件包,令我惊讶的是它似乎正在直接运行.但我无法找到拦截它可能产生的任何消息的方法.使用具有故意错误的greeter.ts文件执行以下操作
tsc greeter.ts > err.log
Run Code Online (Sandbox Code Playgroud)
确认没有重定向.有人知道这方面的方法吗?我想这样做,所以我可以在编辑器中找到错误.
顺便说一句,我注意到tsc只接受小写文件名.做tsc GREETER.TS给错误读取文件"GREETER.TS":找不到文件.
回应Sohnee的评论:不,tsc肯定会发出错误消息,只是我不能重定向它.我的greeter.ts文件,故意错误是:
function greeter(person) {
return "Hello, " + person;
}
var user = "Jane User";
undeclared
document.body.innerHTML = greeter(user);
Run Code Online (Sandbox Code Playgroud)
从OS shell我得到以下内容.
C:\K_F90\F90WX\BOOKS\THENOD~1>tsc greeter.ts
C:/K_F90/F90WX/BOOKS/THENOD~1/greeter.ts(7,0): The name 'undeclared' does not exist in the current scope
Run Code Online (Sandbox Code Playgroud)
但是当我尝试重定向时,我得到:
C:\K_F90\F90WX\BOOKS\THENOD~1>tsc greeter.ts > err.log
C:/K_F90/F90WX/BOOKS/THENOD~1/greeter.ts(7,0): The name 'undeclared' does not exist in the current scope
C:\K_F90\F90WX\BOOKS\THENOD~1>type err.log
C:\K_F90\F90WX\BOOKS\THENOD~1>
Run Code Online (Sandbox Code Playgroud)
我觉得很确定; 正在发出错误消息但未被重定向到err.log,该文件为空.
我发现我的应用程序结构和构建过程中存在使用WebPack,TypeScript和TS-Loader的问题,我认为这是由TypeScript 2.1.4引起的,但显然是在那里.
您可以从我的其他帖子中看到所有详细信息: TypeScript 2.1.4打破了webpack ts-loader中的更改
简而言之,我将Gulp和WebPack设置为/client/app.ts的入口点,现在它几乎没有任何东西(当然没有引用/ server /)但是WebPack构建过程的TypeScript编译阶段仍在尝试在/ server上运行(在我的另一篇文章中,显示来自Server文件夹的编译错误,当它应该只从Client文件夹运行时).
我做错了什么以及如何修复它以便它只在/client/.ts文件上运行,特别是从app.ts中走出结构?
这是我的回购展示了我到目前为止所做的一切:https: //github.com/CmdrShepardsPie/test-ts-app/tree/develop
谢谢
我现在有这个:
export type EVCb = (err:any, val?:any) => void;
export type Task = (cb: EVCb) => void;
export const q = async.queue((task: Task, cb) => task(cb), 2);
Run Code Online (Sandbox Code Playgroud)
有没有办法使用泛型提供有关任务的async.queue类型信息?
这样的事情:
export const q = async.queue<Task>((task: Task, cb) => task(cb), 2);
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚这是否是正确的方法或正确的语法.
我有一个混合TS和JS项目,与正常工作allowJS: true
的环境tsconfig
。
我现在想使用需要的项目引用,composite: true
这意味着allowJS
不能使用。
这是否意味着我只能在纯打字稿项目中使用项目引用?
当我tsc -b
因为不包含 js 文件而运行时,输出代码缺少声明!
我如何仍然在我的项目中包含 js 代码但使用较新的项目引用?
我正在尝试将一个项目转换为 TypeScript,并且我已经设置了大部分内容,但我仍在努力将最后几部分放在一起。我希望能够从普通的 JavaScript 项目中使用这个 TypesScript 项目,所以我的理解是我需要为我现有的源发出 d.ts 文件。我的源目前都是 .js,我们计划随着时间的推移慢慢迁移到 TS。我的问题是让声明与当前的导出/要求语句一起发出。
问题的简单演示:
mod1.js
class MyClass {
constructor(name) {
this.name = name;
}
}
module.exports = {
MyClass,
};
Run Code Online (Sandbox Code Playgroud)
mod2.js
const mod1 = require('./mod1');
module.exports = {
MyClass: mod1.MyClass,
};
Run Code Online (Sandbox Code Playgroud)
一旦我尝试在 mod2 中导出 MyClass 以重新路由一个可以在使用项目时访问 MyClass 的命名空间,我就会得到 Declaration emit for this file requires using private name 'MyClass' from module '"mod1"'. An explicit type annotation may unblock declaration emit.ts(9006)
我们的代码库中有很多重新路由,包含各种类的文件组,然后我们在每个目录级别使用 index.js 文件来定义该命名空间中哪些项目可用,有时还有一堆 UI 元素它们是实例化的类实例,因此我们可以进行如下调用:
const {app, appui} = require('our-cool-app');
app.feature1.doSomething();
appui.component.someButton.click();
Run Code Online (Sandbox Code Playgroud)
是否有一个简单的修复方法可以让我们的 d.ts …
我正在写一个面试问题。我通过创建“Http Client”包来模拟后端服务器。我希望在文件中公开我的一些类型.d.ts
,以便受访者了解上下文,但大多数类型我不这样做,因为它们会暴露模拟的内部工作原理,并为受访者提供一些我希望他们生成的类型。
src/index.ts
import myPackageExport from `./my-package-export`;
export * from `./public-types`;
export { MyPackageExport } from `./my-package-export`;
export default myPackageExport;
Run Code Online (Sandbox Code Playgroud)
我想简单地在我tsconfig.json
和/或我webpack.config.js
喜欢的地方设置一些东西:
tsconfig.json
{
"compilerOptions": {
// ...
"noEmit": true, // I've also tried `false`
"declartion": true,
"emitDeclarationOnly": true,
"rootDir": "./src",
"outDir": "./lib",
},
"include": [ "./src/index.ts" ],
// ...
}
Run Code Online (Sandbox Code Playgroud)
webpack.config.js
// ...
module.exports = {
entry: path.join(__dirName, 'src/index.ts'),
output: {
fileName: 'index.js',
path: path.join(__dirName, 'lib'),
},
// ...
module: {
rules: [
{ …
Run Code Online (Sandbox Code Playgroud) 我正在将 Vite 与 Typescript React 项目一起使用。我的项目中也有笑话测试。
当我运行时vite build
,它似乎正在编译和捆绑我的测试文件。我有一些已被取消的测试,我计划立即进行一些编译错误。
当我运行时yarn build
的例子tsc && vite build
> yarn build
yarn run v1.22.17
warning package.json: No license field
$ tsc && vite build
src/core/utils/numberUtils.spec.ts:1:1 - error TS1208: 'numberUtils.spec.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.
1 describe('numberUtils - ', () => {
~~~~~~~~
Found 1 error.
error Command …
Run Code Online (Sandbox Code Playgroud) Error: Cannot find module \'hello\'
即使 Typescript 编译成功,也会出现节点错误。
Dir 结构(请注意,这与baseUrl 的 tsconfig 文档几乎相同)
\nmain\n| src\n| \xe2\x94\x9c\xe2\x94\x80 index.ts\n| \xe2\x94\x94\xe2\x94\x80 hello\n| \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.ts\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 tsconfig.json\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 package.json\n
Run Code Online (Sandbox Code Playgroud)\n我只想使用非相对导入(所以没有/
, ./
, ../
)。我过去遇到过这个问题,并且总是诉诸于使用相对导入,但我不想继续这样做。
在顶层index.ts
有import { helloWorld } from \'hello\'
。
tsc
编译成功,输出目录dist
如下所示:
main\n\xe2\x94\x9c\xe2\x94\x80 src\n\xe2\x94\x9c\xe2\x94\x80 tsconfig.json\n\xe2\x94\x9c\xe2\x94\x80 package.json\n\xe2\x94\x94\xe2\x94\x80 dist\n \xe2\x94\x9c\xe2\x94\x80 index.js\n \xe2\x94\x94\xe2\x94\x80 hello\n \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 index.js\n
Run Code Online (Sandbox Code Playgroud)\n运行node dist/index.js
输出错误Error: Cannot find module \'hello\'
。import 语句被编译到dist/index.js
抛出require("hello")
错误的位置。
从 …
node.js typescript tsc tsconfig-paths typescript-module-resolution
我有一个 TypeScript 项目,我正在尝试将其转换为使用路径别名的可执行 JavaScript。这是我正在开发的 NPM 包所必需的。
例如,这里从我的lib
目录导入一个方法,而不通过相对路径引用它:
import { hexify } from '@lib/utils/conversion';
Run Code Online (Sandbox Code Playgroud)
通常,当我tsconfig-paths
从入口点运行应用程序时,我会使用类似的命令ts-node-dev --files -r tsconfig-paths/register ./src/index.ts
或在生产模式下使用node -r ts-node/register/transpile-only -r tsconfig-paths/register ./dist/index.js
. 但在这种情况下,我想成功地将其转换为 JavaScript,以便编译器自动将路径别名转换为正确的相对路径,因此不需要使用ts-node
并tsconfig-paths
成功运行 JavaScript 代码。
我的tsconfig.json
文件看起来像这样一些额外的上下文:
{
"ts-node": {
"files": true
},
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"rootDir": "src",
"outDir": "dist",
"moduleResolution": "node",
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": false,
"resolveJsonModule": true,
"baseUrl": …
Run Code Online (Sandbox Code Playgroud) 假设我有这个 monorepo:
/apps
/apps/app1
/apps/app1/src (contains index.ts and many other files and subfolders here)
/apps/app1/tsconfig.json
/apps/app1/package.json
/apps/app2
/apps/app2/src (contains index.ts and many other files and subfolders here)
/apps/app2/tsconfig.json
/apps/app2/package.json
/common
/common/package1
/common/package1/src
/common/package1/src/utility1.ts
/common/package1/src/utility2.ts
/common/package1/tsconfig.json
/common/package1/package.json
/tsconfig.json
/package.json
Run Code Online (Sandbox Code Playgroud)
我只需要 esm 就可以在其中工作。
我怎样才能让 tsx、tsc 或任何其他 ts 工具运行一个或多个使用“common/*/src”中内容的应用程序?基本上不需要将“common”中的内容构建到 dist 文件夹中。
因此,每次我对其中的文件进行更改时,/common
当我在监视模式下运行应用程序时,它们都会立即生效。
tsc ×10
typescript ×9
javascript ×2
tsconfig ×2
webpack ×2
async.js ×1
gulp ×1
monorepo ×1
node.js ×1
npm ×1
ts-loader ×1
tsx ×1
typescript-module-resolution ×1
vite ×1