xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

TypeScript & JSDoc All In One

TypeScript & JSDoc All In One

.d.ts

Setting up your Project to emit .d.ts files

To add the creation of .d.ts files in your project, you will need to do up to four steps:

  1. Add TypeScript to your dev dependencies
  2. Add a tsconfig.json to configure TypeScript
  3. Run the TypeScript compiler(tsc -d) to generate the corresponding d.ts files for JS files
  4. (optional) Edit your package.json to reference the types

tsd

https://github.com/SamVerschueren/tsd

jsonc, JSON with Comments

{
  // Change this to match your project
  "include": ["src/**/*"],
  "compilerOptions": {
    // Tells TypeScript to read JS files, as
    // normally they are ignored as source files
    "allowJs": true,
    // Generate d.ts files
    "declaration": true,
    // This compiler run should
    // only output d.ts files
    "emitDeclarationOnly": true,
    // Types should go into this directory.
    // Removing this would place the .d.ts files
    // next to the .js files
    "outDir": "dist",
    // go to js file when using IDE functions like
    // "Go to Definition" in VSCode
    "declarationMap": true
  }
}

JSON5

{
  // comments
  unquoted: 'and you can quote me on that',
  singleQuotes: 'I can use "double quotes" here',
  lineBreaks: "Look, Mom! \
No \\n's!",
  hexadecimal: 0xdecaf,
  leadingDecimalPoint: .8675309, andTrailing: 8675309.,
  positiveSign: +1,
  trailingComma: 'in objects', andIn: ['arrays',],
  "backwardsCompatible": "with JSON",
}

https://json5.org/

https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html

@type

https://www.typescriptlang.org/docs/handbook/type-checking-javascript-files.html#supported-jsdoc

class C {
  constructor() {
    /** @type {number | undefined} */
    this.prop = undefined;
    /** @type {number | undefined} */
    this.count;
  }
}
 
let c = new C();
c.prop = 0;
// OK
c.count = "string";
// Type 'string' is not assignable to type 'number'. ❌

try bug

allowJs bug ❌

error TS6059: File '/rollup.config.js' is not under 'rootDir'

// rollup.config.js ❓not at all
import * as pkg from "./package.json";

solution ✅ exclude folder & files

{
  "compilerOptions": {
    "target": "es2015",
    "module": "esnext",
    "moduleResolution": "node",
    // Tells TypeScript to read JS files, as normally they are ignored as source files
    "allowJs": true,
    // Generate d.ts files
    "declaration": true,
    // go to js file when using IDE functions like "Go to Definition" in VSCode
    "declarationMap": true,
    // This compiler run should only output d.ts files
    "emitDeclarationOnly": true,
    "sourceMap": true,
    "esModuleInterop": true,
    "rootDir": "./src/",
    "outDir": "./dist/",
    "strict": true,
    "experimentalDecorators": true,
    "useDefineForClassFields": false,
    "skipLibCheck": true, 
    "forceConsistentCasingInFileNames": true
  },
  "exclude":[
    "rollup.config.js",
    "test",
    "dist",
    "node_modules",
  ],
}


https://stackoverflow.com/questions/40579191/unexpected-behaviour-of-rootdir-and-outdir/40752115#40752115

https://stackoverflow.com/questions/57422458/error-ts6059-file-is-not-under-rootdir-rootdir-is-expected-to-contain-al

https://github.com/microsoft/TypeScript/issues/30693

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-07-27 22:25  xgqfrms  阅读(120)  评论(1编辑  收藏  举报