我正在阅读有关路径映射的内容tsconfig.json
,我想使用它来避免使用以下丑陋的路径:
项目组织有点奇怪,因为我们有一个包含项目和库的单一存储库.项目按公司和浏览器/服务器/通用分组.
如何配置路径tsconfig.json
而不是:
import { Something } from "../../../../../lib/src/[browser/server/universal]/...";
Run Code Online (Sandbox Code Playgroud)
我可以用:
import { Something } from "lib/src/[browser/server/universal]/...";
Run Code Online (Sandbox Code Playgroud)
webpack配置中还需要其他东西吗?还是tsconfig.json
足够了?
我正在使用 Vite 创建一个新的 React + TypeScript 项目。
创建项目后,根文件夹中有两个 TypeScript 配置文件:tsconfig.json
和tsconfig.node.json
. 以下是每一篇的内容:
{
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"allowJs": false,
"skipLibCheck": false,
"esModuleInterop": false,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"module": "ESNext",
"moduleResolution": "Node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": ["src"],
"references": [{ "path": "./tsconfig.node.json" }]
}
Run Code Online (Sandbox Code Playgroud)
{
"compilerOptions": {
"composite": true,
"module": "esnext",
"moduleResolution": "node"
},
"include": ["vite.config.ts"]
}
Run Code Online (Sandbox Code Playgroud)
为什么我们需要两个?
第二个是做什么的?
我可以删除第二个吗?
我正在努力解决 VSCode 中的“错误”:
Argument for '--jsx' option must be: 'preserve', 'react-native', 'react'
Run Code Online (Sandbox Code Playgroud)
因此,当value 似乎有效时,react-scripts (create-react-app) 会自动将jsx
key设置为react-jsx
value react
。
实际上,代码运行良好并显示了我想要的页面,但是 IDE 将所有内容都强调为错误,并说:
Cannot use JSX unless the '--jsx' flag is provided.
Run Code Online (Sandbox Code Playgroud)
这是我的 tsconfig.json :
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
} …
Run Code Online (Sandbox Code Playgroud) 我对Typescript很新.tsconfig.json中的Target表示什么?
{
"compilerOptions":
{
"sourceMap": true,
"target": "es5",
"module": "commonjs",
"jsx": "react",
"moduleResolution": "classic",
"lib": [ "es2015", "dom", "es2017" ]
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 Remix (remix.run) 设置一个新项目,并尝试配置 Cypress/ESLint 进行组件测试。我有TestComponent.cy.ts
一些样板代码:
describe('TestComponent.cy.ts', () => {
it('playground', () => {
// cy.mount()
})
})
Run Code Online (Sandbox Code Playgroud)
但是,该describe
函数会抛出此错误:
Parsing error: ESLint was configured to run on `<tsconfigRootDir>/component/TestComponent.cy.ts` using `parserOptions.project`: <tsconfigRootDir>/../../../../../../users/tduke/desktop/dev/blog/cypress/tsconfig.json
However, that TSConfig does not include this file. Either:
- Change ESLint's list of included files to not include this file
- Change that TSConfig to include this file
- Create a new TSConfig that includes this file and include it in your parserOptions.project
Run Code Online (Sandbox Code Playgroud)
我尝试重新配置我的 …
假设我把我的代码放在下面src
并测试spec
:
+ spec
+ --- classA.spec.ts
+ src
+ --- classA.ts
+ --- classB.ts
+ --- index.ts
+ tsconfig.json
Run Code Online (Sandbox Code Playgroud)
我只想转发src
到dist
文件夹.既然index.ts
是我的包的入口点,我的tsconfig.json
样子如下:
{
"compileOptions": {
"module": "commonjs"
"outDir": "dist"
},
"files": {
"src/index.ts",
"typings/main.d.ts"
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这tsconfig.json
不包括测试文件,因此我无法解析其中的依赖项.
另一方面,如果我将测试文件包含在内,tsconfig.json
那么它们也会被转换为dist
文件夹.
我该如何解决这个问题?
是否有可能覆盖tsconfig.json
从mocha调用时使用的ts节点?
我的主要tsconfig.json
包含"module": "es2015"
,但我只想"module": "commonjs"
用于ts节点.
我试过这个
mocha --compilers ts:ts-node/register,tsx:ts-node/register \
--compilerOptions '{"module":"commonjs"}' \
--require ts-node/register test/**/*.spec.ts*
Run Code Online (Sandbox Code Playgroud)
但它不起作用:
SyntaxError: Unexpected token import
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:387:25)
at Module.m._compile (/usr/lib/node_modules/ts-node/src/index.ts:406:23)
at Module._extensions..js (module.js:422:10)
at Object.require.extensions.(anonymous function) [as .tsx] (/usr/lib/node_modules/ts-node/src/index.ts:409:12)
at Module.load (module.js:357:32)
at Function.Module._load (module.js:314:12)
at Module.require (module.js:367:17)
at require (internal/module.js:16:19)
at /usr/lib/node_modules/mocha/lib/mocha.js:222:27
at Array.forEach (native)
at Mocha.loadFiles (/usr/lib/node_modules/mocha/lib/mocha.js:219:14)
at Mocha.run (/usr/lib/node_modules/mocha/lib/mocha.js:487:10)
at Object.<anonymous> (/usr/lib/node_modules/mocha/bin/_mocha:458:18)
at Module._compile (module.js:413:34)
at Object.Module._extensions..js (module.js:422:10)
at Module.load …
Run Code Online (Sandbox Code Playgroud) 我正在尝试为我的模拟服务器设置别名。每当我尝试编译ts
文件时,它都会返回找不到正确模块的错误,即使这些模块是在tsconfig,json
->中定义的paths
文件夹结构:
??? server
? ??? src
? ???/json
??? src
? ???/modules
??? tsconfig.json
Run Code Online (Sandbox Code Playgroud)
这是我的 tsconfig.json
{
"compilerOptions": {
"baseUrl": "./src",
"experimentalDecorators": true,
"jsx": "react",
"lib": [
"dom",
"es2015",
"es2015.promise"
],
"module": "commonjs",
"moduleResolution": "node",
"noImplicitAny": true,
"noUnusedLocals": true,
"esModuleInterop": true,
"paths": {
"@project/app/modules/*": [
"modules/*"
],
"@project/server/data/*": [
"../server/src/json/*"
]
},
"sourceMap": true,
"target": "es5"
},
"exclude": [
"node_modules",
"tools"
]
}
Run Code Online (Sandbox Code Playgroud)
错误:
Error: Cannot find module '@project/server/data/accounts/accountsList'
我正在使用Visual Studio Code并且具有相当常见的项目结构:
??? client/
? ??? tsconfig.json
??? shared/
??? server/
? ??? tsconfig.json
??? project.json
Run Code Online (Sandbox Code Playgroud)
这两个tsconfig文件具有不同的设置(例如,client/
目标ES5 下的设置,server/
目标ES6下的设置).
问题是我希望共享目录包含在两个项目中.我不能使用tsconfig这样做,因为该exclude
选项不允许我包含一个比tsconfig.json更高的目录中的文件夹,并且使用files
我必须不断保持文件列表是最新的,因为它不支持全球.
请注意,我可以通过将共享文件夹添加到tsc中来编译,我想要的是Visual Studio Code IDE识别intellisense的共享代码等.
是等待filesGlob的唯一选择吗?
我有打字稿编译的问题。smbd 遇到过吗?
node_modules/@types/node/index.d.ts(20,1): 错误 TS1084: 无效的“引用”指令语法。
tsconfig.json:
{
"compileOnSave": false,
"compilerOptions": {
"sourceMap": true,
"outDir": "./dist",
"rootDir": "./app",
"target": "es6",
"module": "commonjs",
"declaration": false,
"noImplicitAny": false,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"listFiles": false,
"skipLibCheck": true
},
"include": [
"./app/**/*.ts"
]
}
Run Code Online (Sandbox Code Playgroud)
package.json 中的打字稿版本: "typescript": "^2.6.1"
tsconfig ×10
typescript ×9
javascript ×2
node.js ×2
alias ×1
compilation ×1
jsx ×1
mocha.js ×1
node-modules ×1
reactjs ×1
remix ×1
transpiler ×1
ts-node ×1
tsc ×1
vite ×1