在我的package.json指向本地包my-custom-i18n的相对路径:
"dependencies": {
"core-js": "^2.4.1",
"my-custom-i18n": "./../MyProject.Shared/myproject-i18n",
"rxjs": "5.0.0-beta.12",
...
}
Run Code Online (Sandbox Code Playgroud)
npm install正确安装包,但yarn有问题,只是找不到这个包:
$ yarn
yarn install v0.15.1
info No lockfile found.
[1/4] Resolving packages...
error Couldn't find package "myproject-i18n" on the "npm" registry.
info Visit http://yarnpkg.com/en/docs/cli/install for documentation about this command.
Run Code Online (Sandbox Code Playgroud)
我看到它在npm注册表中看起来,这个包不存在.
使用当地包装纱线有什么变化吗?通过本地包我的意思是相对路径指向的包my-custom-i18n.
我有一个npm包,写得不好,过时的打字.我写了自己的打字,现在我想知道我是否能以某种方式排除npm包中的原始打字.它不是接口的简单扩展,原点基本上是垃圾.
当然,使用tsconfig.json中的排除列表不能用于此目的,因为即使您排除该文件夹,它仍会从node_modules加载文件.
我是 TypeScript 的新手,我在一个只是为了练习而制作的小应用程序中使用 Passport.js 实现了用户身份验证。
\n问题
\n我试图告诉 Passport.js 将用户属性保存id到会话中以进行进一步身份验证,但我只是找不到一种方法来告诉 TypeScript 该id属性user存在。
passport.serializeUser((user, done) => {\n done(undefined, user.id); // Property 'id' does not exists on type 'User' \n});\nRun Code Online (Sandbox Code Playgroud)\n该参数user显示为类型Express.User,但它实际上是 TypeOrm 实体的实例User。通过搜索帖子,我发现我可以扩展类型Express.User以添加我需要使用的属性:
// src/custom-typings/index.d.ts\n namespace Express {\n interface User {\n id?: number;\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n这样做可以消除编码时的错误警告,但是当我尝试编译代码时,错误再次出现,但在我的 VSCode 终端中!
\nsrc/core/passport.ts:38:24 - error TS2339: Property 'id' does not exist on type 'User'.\n\n38 done(undefined, user.id);\n …Run Code Online (Sandbox Code Playgroud) 我正在使用一个foo在DefinitelyTyped上不存在的npm包.换句话说,@types/foo不存在(或可能已过时!)
我仍然希望能够在更严格的设置下使用它noImplicitAny,所以我需要自己编写自定义定义文件.最后,我想向DefinitelyTyped发送一个pull请求,以便此文件对我项目之外的其他人有用.
有很简单的解决方案,比如创建一个名为的全局文件./src/types.d.ts,我可以编写以下内容
declare module "foo" {
export function hello(): void;
export function world(): void;
}
Run Code Online (Sandbox Code Playgroud)
但是如果我使用那种语法,当我将它提交给DefinitelyTyped时,我可能需要重写我的模块.
如何构建我的项目以便我可以轻松地创作然后将本地.d.ts文件发送到DefinitelyTyped?
我最近将我的项目从 3.9.9 升级到 TypeScript 4.4.3。
"strictNullChecks": true,我的项目在其 中使用tsconfig.json,并在浏览器中运行,而不是在 Node.js 上的服务器端运行。
在 TypeScript 4.4.3 中,似乎类型声明top已更改为WindowProxy | null( node_modules/typescript/lib/lib.dom.d.ts)
这意味着无论我尝试访问2的属性,我都会收到以下错误1 :TS Playgroundtop
const topUrl = top.window.location.href; // => Object is possibly 'null'.
Run Code Online (Sandbox Code Playgroud)
如何仅在可能为空时忽略此类错误top?3
1据我所知,此错误警告我注意我的网站在 iframe 中加载的情况,因此top由于 XSS 而无法访问。这不是问题,因为我的'X-Frame-Options'设置为'sameorigin',因此将拒绝在跨源 iframe 中加载我的网站。
2我访问 的属性,top因为我在项目中经常使用 iframe,它会加载同一域上的子页面。
3我可以使用以下修复来解决这个问题Object is possibly 'null'.,但我不想这样做,因为我的项目相当大,而且这个修复将很乏味,而且改进很少。
let topUrl = top?.window.location.href || ''; …Run Code Online (Sandbox Code Playgroud) 我有一个错误的node_module(angularfire2),它没有使用另一个node_module(@ firebase)中的新版本更新.
我正在尝试让tsconfig.json帮助我为它设置一个路径别名,以便将其重新路由以解析为在src中编写的调整文件而不是@ firebase的node_modules中的不兼容的输入文件作为临时修复.
我知道我可以降级(@firebase)node_module以使其兼容.然而,这个问题并不是要让它发挥作用.我只是想弄清楚如何能够覆盖node_module坏的打字.
我正在使用Angular的cli项目,希望我不需要弹出webpack来控制它.
我从这篇文章中了解到,我覆盖了@types文件夹中的打字.
但是我仍然无法在node_module本身中使用index.d.ts覆盖打字输入.
例如(来自angularfire2)
import { FirebaseApp as FBApp } from '@firebase/app-types';
Run Code Online (Sandbox Code Playgroud)
我想@firebase/app-types在我的tsconfig.json中创建一个别名,以便angularfire2可以查看src/types-overwrite/@firebase/app-types.
我有以下tsconfig.json但它仍然不会正确执行别名,并仍将解析为不兼容的node_module的输入文件而不是src中的文件.
我的tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"outDir": "./dist/out-tsc",
"sourceMap": true,
"declaration": false,
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"target": "es5",
"typeRoots": [
"node_modules/@types",
"src/types-overwrite"
],
"paths": {
"@firebase/app-types": ["src/types-overwrite/@firebase/app-types"]
},
"lib": [
"es2017",
"dom"
]
},
"include": [
"node_modules/angularfire2",
]
}
Run Code Online (Sandbox Code Playgroud)
如何在Angular-CLI项目的node_module中覆盖index.d.ts键入文件,或者如何让我的tsconfig.json工作?
我添加了一个存储库来展示问题:
typescript ×6
javascript ×2
node.js ×2
npm ×2
tsconfig ×2
angular-cli ×1
node-modules ×1
passport.js ×1
yarnpkg ×1