This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-05-31
Channels
- # announcements (1)
- # babashka (11)
- # beginners (33)
- # calva (11)
- # clj-kondo (6)
- # clojars (8)
- # clojure (28)
- # clojure-europe (16)
- # clojure-nl (1)
- # clojure-norway (6)
- # clojure-sweden (23)
- # clojure-uk (5)
- # clr (10)
- # gratitude (6)
- # hyperfiddle (14)
- # jobs-discuss (1)
- # lsp (7)
- # pathom (6)
- # pedestal (3)
- # polylith (4)
- # ring (4)
- # specter (6)
- # squint (8)
Hello! Some time ago I mentioned that I wanted to make typescript declarations understandable to clj-kondo, so that there is static analysis in Clojurescript. Is clj-kondo the right tool to build for? If I understood correctly, there doesn't seem to be union types or other (somewhat advanced) stuff and I was wondering how I should approach this. My initial idea is to convert those *.d.ts files into malli schemas and then do something with that. I was also thinking maybe clojure-lsp is the way, it seems like static analysis could fit there nicely. I haven't studied LSP in depth but I wouldn't mind doing that. Typescript provides an API to get declaration ASTs which can be used to extract types, so the hardest part would be to translate that stuff to Clojurescript. Typescript also provides typings for the entire web api and it could be handy to leverage that. I would appreciate any comments that could help me steer this plan.
> there doesn't seem to be union types
yes, there are, just use sets: #{:string :keyword}
this is the current documentation: https://github.com/clj-kondo/clj-kondo/blob/master/doc/types.md
@U0479UCF48H The bindings don't change that often so I could just run js (or cljs) code that uses typescript compiler API to retrieve typings that clj-kondo understands and once that is done, you can use the regular cljs tools.
To see browser api bindings, make node project, npm install typescript
and check out ./node_modules/typescript/lib
. Typings are spread out in several files, for example DOM is typed in lib.dom.d.ts
If you want a simple way to look at the AST, you can use https://ts-ast-viewer.com/#
Here's some node repl code to get you started:
ts = require("typescript")
dom_typings = "node_modules/typescript/lib/lib.dom.d.ts"
node = ts.createProgram([dom_typings], {}).getSourceFile(dom_typings) // long output
You can traverse the tree with node.forEachChild( lambda )
or node.getChildren()
. You can find extra info about compiler API in https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API .and typescript.d.ts
has the typings for the AST which can be handy to decipher enums.