Create TypeScript definition files from an existing JavaScript library.
We've encountered the excellent oidc-client-js and would like to create its TypeScript definition file. Here has been our approach.
Open PowerShell. Enter the
src
directory. Run this:
Get-ChildItem -Path *.js | foreach { Copy-Item $_ $_.Name.Replace(".js", ".temp.ts") }
Get-ChildItem -Path *.temp.ts | foreach { tsc --declaration $_.Name }
Get-ChildItem -Path *.d.ts -Exclude "_index.d.ts" | `
foreach { Get-Content $_ } | `
where { !$_.ToString().StartsWith("import") } | `
foreach { $_.Replace("export default", "export") } | `
foreach { Add-Content _index.d.ts $_ }
Remove-Item *.temp.ts, *.temp.d.ts, *.temp.js
There will be some errors, but we can ignore those; it still ends with a nice
index.d.ts
file that includes all the definitions.