npm TypeScript库/SDK -安装自己的包后,无法识别类型

0kjbasz6  于 2023-08-06  发布在  TypeScript
关注(0)|答案(2)|浏览(138)

我使用typescript-starter(https://bitjson.github.io/typescript-starter/)开发了自定义的typescript库。
这是我的类型文件夹结构
x1c 0d1x的数据
现在,在安装了这个包之后,我尝试了一个函数或服务调用,如下所示



如图所示,我有requestbody模型作为参数,但当我试图指定字段时,属性没有显示。
我现在该怎么做才能解决这个问题?
我根据一些SO网站和GPT答案尝试了什么:
1.尝试重新启动VS Code,因为它可能是intellisense问题-没有运气
下面是构建输出。它包含相同文件夹格式的所需类型文件。



package.json文件

  1. "scripts": {
  2. "build": "run-p build:* && cpy README.md build",
  3. "build:main": "tsc -p tsconfig.json && cpy README.md build",
  4. "build:module": "tsc -p tsconfig.module.json && cpy README.md build",
  5. "fix": "run-s fix:*",
  6. "fix:prettier": "prettier \"src/**/*.ts\" --write",
  7. "fix:lint": "eslint src --ext .ts --fix",
  8. "test": "run-s build test:*",
  9. "test:lint": "eslint src --ext .ts",
  10. "test:unit": "nyc --silent ava",
  11. "check-cli": "run-s test diff-integration-tests check-integration-tests",
  12. "check-integration-tests": "run-s check-integration-test:*",
  13. "diff-integration-tests": "mkdir -p diff && rm -rf diff/test && cp -r test diff/test && rm -rf diff/test/test-*/.git && cd diff && git init --quiet && git add -A && git commit --quiet --no-verify --allow-empty -m 'WIP' && echo '\\n\\nCommitted most recent integration test output in the \"diff\" directory. Review the changes with \"cd diff && git diff HEAD\" or your preferred git diff viewer.'",
  14. "watch:build": "tsc -p tsconfig.json -w",
  15. "watch:test": "nyc --silent ava --watch",
  16. "cov": "run-s build test:unit cov:html cov:lcov && open-cli coverage/index.html",
  17. "cov:html": "nyc report --reporter=html",
  18. "cov:lcov": "nyc report --reporter=lcov",
  19. "cov:send": "run-s cov:lcov && codecov",
  20. "cov:check": "nyc report && nyc check-coverage --lines 100 --functions 100 --branches 100",
  21. "doc": "run-s doc:html && open-cli build/docs/index.html",
  22. "doc:html": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --out build/docs",
  23. "doc:json": "typedoc src/ --exclude **/*.spec.ts --target ES6 --mode file --json build/docs/typedoc.json",
  24. "doc:publish": "gh-pages -m \"[ci skip] Updates\" -d build/docs",
  25. "version": "standard-version",
  26. "reset-hard": "git clean -dfx && git reset --hard && npm i",
  27. "prepare-release": "run-s reset-hard test cov:check doc:html version doc:publish"
  28. },
  29. "engines": {
  30. "node": ">=10"
  31. },
  32. "devDependencies": {
  33. "@ava/typescript": "^1.1.1",
  34. "@istanbuljs/nyc-config-typescript": "^1.0.1",
  35. "@typescript-eslint/eslint-plugin": "^4.0.1",
  36. "@typescript-eslint/parser": "^4.0.1",
  37. "ava": "^5.2.0",
  38. "codecov": "^3.5.0",
  39. "cpy-cli": "^5.0.0",
  40. "cspell": "^4.1.0",
  41. "cz-conventional-changelog": "^3.3.0",
  42. "eslint": "^7.8.0",
  43. "eslint-config-prettier": "^6.11.0",
  44. "eslint-plugin-eslint-comments": "^3.2.0",
  45. "eslint-plugin-functional": "^3.0.2",
  46. "eslint-plugin-import": "^2.22.0",
  47. "gh-pages": "^3.1.0",
  48. "npm-run-all": "^4.1.5",
  49. "nyc": "^15.1.0",
  50. "open-cli": "^7.2.0",
  51. "prettier": "^2.1.1",
  52. "standard-version": "^9.0.0",
  53. "ts-node": "^9.0.0",
  54. "typedoc": "^0.24.7",
  55. "typescript": "^4.0.2"
  56. },
  57. "files": [
  58. "build",
  59. "!**/*.spec.*",
  60. "!**/*.json",
  61. "CHANGELOG.md",
  62. "LICENSE",
  63. "README.md"
  64. ],
  65. "ava": {
  66. "failFast": true,
  67. "timeout": "60s",
  68. "typescript": {
  69. "rewritePaths": {
  70. "src/": "build/main/"
  71. }
  72. },
  73. "files": [
  74. "!build/module/**"
  75. ]
  76. },
  77. "config": {
  78. "commitizen": {
  79. "path": "cz-conventional-changelog"
  80. }
  81. },
  82. "prettier": {
  83. "singleQuote": true
  84. },
  85. "nyc": {
  86. "extends": "@istanbuljs/nyc-config-typescript",
  87. "exclude": [
  88. "**/*.spec.js"
  89. ]
  90. },
  91. "dependencies": {
  92. "axios": "^1.4.0",
  93. "dotenv": "^16.3.1",
  94. "form-data": "^4.0.0",
  95. "pino": "^8.14.1"
  96. }

字符串
tsconfig.json文件

  1. {
  2. "compilerOptions": {
  3. "incremental": true,
  4. "target": "es2017",
  5. "outDir": "build/main",
  6. "rootDir": "src",
  7. "moduleResolution": "node",
  8. "module": "commonjs",
  9. "declaration": true,
  10. "inlineSourceMap": true,
  11. "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */,
  12. "resolveJsonModule": true /* Include modules imported with .json extension. */,
  13. // "strict": true /* Enable all strict type-checking options. */,
  14. /* Strict Type-Checking Options */
  15. // "noImplicitAny": true /* Raise error on expressions and declarations with an implied 'any' type. */,
  16. // "strictNullChecks": true /* Enable strict null checks. */,
  17. // "strictFunctionTypes": true /* Enable strict checking of function types. */,
  18. // "strictPropertyInitialization": true /* Enable strict checking of property initialization in classes. */,
  19. // "noImplicitThis": true /* Raise error on 'this' expressions with an implied 'any' type. */,
  20. // "alwaysStrict": true /* Parse in strict mode and emit "use strict" for each source file. */,
  21. /* Additional Checks */
  22. "noUnusedLocals": false /* Report errors on unused locals. */,
  23. "noUnusedParameters": false /* Report errors on unused parameters. */,
  24. "noImplicitReturns": true /* Report error when not all code paths in function return a value. */,
  25. "noFallthroughCasesInSwitch": true /* Report errors for fallthrough cases in switch statement. */,
  26. /* Debugging Options */
  27. "traceResolution": false /* Report module resolution log messages. */,
  28. "listEmittedFiles": false /* Print names of generated files part of the compilation. */,
  29. "listFiles": false /* Print names of files part of the compilation. */,
  30. "pretty": true /* Stylize errors and messages using color and context. */,
  31. /* Experimental Options */
  32. // "experimentalDecorators": true /* Enables experimental support for ES7 decorators. */,
  33. // "emitDecoratorMetadata": true /* Enables experimental support for emitting type metadata for decorators. */,
  34. "lib": [
  35. "es2017",
  36. "dom"
  37. ],
  38. "types": [
  39. "node"
  40. ],
  41. "typeRoots": [
  42. "node_modules/@types",
  43. "src/types"
  44. ]
  45. },
  46. "include": [
  47. "src/**/*.ts"
  48. ],
  49. "exclude": [
  50. "node_modules/**"
  51. ],
  52. "compileOnSave": false
  53. }

7y4bm7vi

7y4bm7vi1#

您需要导出参数的类型。在这种情况下,找到GetAppMenuAppRole类型的类型定义,并确保它前面有export。例如:

  1. export interface GetAppMenuAppRole {
  2. field1: string;
  3. }

字符串

lskq00tm

lskq00tm2#

简体中文
你可以像这样使用require导入文件:

  1. const myfile = require("myfile");
  2. //path to your file
  3. const {xyz, example} = myfile.xyz()
  4. // xyz = 1, examplr = text

字符串
myfile.js的内容:

  1. function xyz() {
  2. const x = "text"
  3. function example() {
  4. return 1
  5. }
  6. const y = example()
  7. return {xyz:x,example:y}
  8. }


希望这能帮上忙。

展开查看全部

相关问题