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:
- Add TypeScript to your
dev dependencies
- Add a
tsconfig.json
to configure TypeScript - Run the TypeScript compiler(
tsc -d
) to generate the correspondingd.ts
files for JS files - (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://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://github.com/microsoft/TypeScript/issues/30693
refs
©xgqfrms 2012-2020
www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!
本文首发于博客园,作者:xgqfrms,原文链接:https://www.cnblogs.com/xgqfrms/p/16526785.html
未经授权禁止转载,违者必究!