Welcome to Shaun Luttin's public notebook. It contains rough, practical notes. The guiding idea is that, despite what marketing tells us, there are no experts at anything. Sharing our half-baked ideas helps everyone. We're all just muddling thru. Find out more about our work at bigfont.ca.

Create TypeScript definition files from an existing JavaScript library.

Tags: typescript, powershell

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.