相关疑难解决方法(0)

如何使用typescript设置require.js配置?

好的,我一直在阅读很多关于此的问题和答案,其中很多都是垃圾.

我有一个非常简单的问题.我该怎么做相当于这个:

require.config({
    paths: {
        "blah": '/libs/blah/blah',
    }
}); 
require(['blah'], function(b) {
    console.log(b); 
});
Run Code Online (Sandbox Code Playgroud)

打字稿?

这不起作用:

declare var require;
require.config({
    paths: {
        "blah": '/libs/blah/blah',
    }
});
import b = require('blah');
console.log(b);

s.ts(8,1): error TS2071: Unable to resolve external module ''blah''.
s.ts(8,1): error TS2072: Module cannot be aliased to a non-module type.
error TS5037: Cannot compile external modules unless the '--module' flag is provided.
Run Code Online (Sandbox Code Playgroud)

使用--module标志进行编译,使用虚拟blah.ts shim编译,但输出为:

define(["require", "exports", 'blah'], function(require, exports, b) {
    require.config({
        paths: {
            "blah": '/libs/blah/blah'
        }
    });

    console.log(b); …
Run Code Online (Sandbox Code Playgroud)

requirejs typescript

24
推荐指数
2
解决办法
2万
查看次数

电子和打字稿"找不到模块'电子'"

关于https://electron.atom.io/blog/2017/06/01/typescript 电子支持打字稿但是我的设置无效:

[ts]无法找到模块'电子'

我使用vscode 1.16.1

这是我的package.json

{
  [...]
  "devDependencies": {
    "electron": "^1.6.13",
    "ts-loader": "~2.3.7",
    "typescript": "~2.5.0",
    "webpack": "^3.6.0",
    [...]
  }
}
Run Code Online (Sandbox Code Playgroud)

tsconfig.json

{
    "compilerOptions": {
        "module": "es6",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": true
    },
    "include": [
        "src/**/*"
    ]
}
Run Code Online (Sandbox Code Playgroud)

和我的网络包

const path = require('path');

module.exports = [{
  entry: './src/main.ts',
  devtool: 'inline-source-map',
  target: 'electron',
  module: {
    rules: [
      { test: /\.ts$/, use: 'ts-loader', exclude: /node_modules/ }
    ]
  },
  node: {
    __dirname: false,
    __filename: false
  },
  resolve: {
    extensions: [".ts", …
Run Code Online (Sandbox Code Playgroud)

typescript electron

11
推荐指数
1
解决办法
3228
查看次数

如何引用另一个TypeScript文件中的类?

如何.ts在TypeScript中访问另一个文件中的类?

例如

app.ts

window.onload = () => {
   /// <reference path="TilingEngine/SofRenderer.ts" >
   var drawingTool = new SofRenderer();
};
Run Code Online (Sandbox Code Playgroud)

TilingEngine/SofRenderer.ts

class SofRenderer {
}
Run Code Online (Sandbox Code Playgroud)

无法编译,错误:

找不到符号'SofRenderer'.

也可以看看

typescript

7
推荐指数
1
解决办法
1万
查看次数

在编译为js后,Typescript无法正确解析模块

我在tsconfig.json文件中设置属性路径,如:

"paths": {
  "*": [
    "*",
    "src/*",
    "node_modules/*"
  ],
  "src/*": [ "./src/*" ]
},
Run Code Online (Sandbox Code Playgroud)

它让我能够更轻松地采取一些模块:例如

- src
  |- moduleA
  |- utils
    |- moduleB

// moduleA
import { something } from 'utils/moduleB'
Run Code Online (Sandbox Code Playgroud)

但是在编译之后我得到了moduleB.js中的下一个路径:

something = require('utils/moduleB')
Run Code Online (Sandbox Code Playgroud)

而不是相对路径:

something = require('./utils/moduleB')
Run Code Online (Sandbox Code Playgroud)

它在Node下不起作用,因为Node模块解析系统对utils文件夹一无所知.

那么,我如何强制tsc在这里使用相对路径?

更新:这是我生成的js文件的真实示例:

在此输入图像描述

data&utils它们是内部模块而非外部模块.我的问题是为什么tsc不能根据编译文件中的baseUrl解析它们

javascript node.js typescript

5
推荐指数
0
解决办法
238
查看次数

标签 统计

typescript ×4

electron ×1

javascript ×1

node.js ×1

requirejs ×1