Skip to content

Commit

Permalink
adds example of how to generate TypeScript type declaration files. Is…
Browse files Browse the repository at this point in the history
…sue #24
  • Loading branch information
rsms committed May 12, 2021
1 parent 0e45a10 commit 23dcb87
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/typedef-generation/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This example shows how to generate TypeScript ".d.ts" files from .ts source files

```
$ ./build.js
```

Compiled .js bundle can now be found in `./out/` together with `main.d.ts`
32 changes: 32 additions & 0 deletions examples/typedef-generation/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#!/usr/bin/env node
const { build, ts, tsconfig, dirname, glob, log } = require("estrella")

build({
entry: "main.ts",
outfile: "out/foo.js",
onEnd(config) {
const dtsFilesOutdir = dirname(config.outfile)
generateTypeDefs(tsconfig(config), config.entry, dtsFilesOutdir)
},
})

function generateTypeDefs(tsconfig, entryfiles, outdir) {
const filenames = Array.from(
new Set(
(Array.isArray(entryfiles) ? entryfiles : [entryfiles])
.concat(tsconfig.include || []))).filter(v => v)
log.info("Generating type declaration files for", filenames.join(", "))
const compilerOptions = {
...tsconfig.compilerOptions,
moduleResolution: undefined,
declaration: true,
outDir: outdir,
}
const program = ts.ts.createProgram(filenames, compilerOptions)
const targetSourceFile = undefined
const writeFile = undefined
const cancellationToken = undefined
const emitOnlyDtsFiles = true
program.emit(targetSourceFile, writeFile, cancellationToken, emitOnlyDtsFiles)
log.info("Wrote", glob(outdir + "/*.d.ts").join(", "))
}
14 changes: 14 additions & 0 deletions examples/typedef-generation/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
export interface Named {
readonly name :string
}

export class Foo implements Named {
readonly name :string
}

export class Bar extends Foo {
readonly id :number
getName() :string {
return this.name
}
}
5 changes: 5 additions & 0 deletions examples/typedef-generation/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/sh
set -e
rm -rf out
./build.js -no-diag
head -n3 out/main.d.ts
20 changes: 20 additions & 0 deletions examples/typedef-generation/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"compilerOptions": {
"noImplicitAny": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"alwaysStrict": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
// "strictNullChecks": true, "strictPropertyInitialization": true,
"moduleResolution": "node",
"target": "es2017",
"baseUrl": "src"
},
"include": [
"main.ts"
]
}

0 comments on commit 23dcb87

Please sign in to comment.