Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Type Safe "Everything"

Type Safe "Everything"

andoshin11

March 04, 2020
Tweet

More Decks by andoshin11

Other Decks in Technology

Transcript

  1. Type Safe
    “EVERYTHING”
    @andoshin11

    4. 3. 2020

    #tsc_api_study


    View Slide

  2. Who am I ?
    Shin Ando (a.k.a Andy)
    @andoshin11
    - Freelance Full-Stack Developer
    - TypeScript / Vue.js / React / Go / Ruby / Flutter / Terraform / etc…
    - ❤ Code Generation!

    View Slide

  3. ● Powerful editor completion
    ● Easy to analyze code base using AST
    ● Type-level programming
    ● Overall durability
    ● Maintainable
    ● Scalable
    ● F**king AWESOME!!
    We all love TypeScript, eh? ❤

    View Slide

  4. However...

    View Slide

  5. Not everyone speaks TypeScript (just yet...)

    View Slide

  6. Not everyone speaks TypeScript (just yet...)
    ● Other programming languages (Go, Ruby, Python, Java, etc...)
    ● Query based languages (a.k.a Database)
    ● Statically defined specs (YAML, CSV, Markdown, Excel)
    ● etc...

    View Slide

  7. Not everyone speaks TypeScript (just yet...)
    ● Other programming languages (Go, Ruby, Python, Java, etc...)
    ● Query based languages (a.k.a Database)
    ● Statically defined specs (YAML, CSV, Markdown, Excel)
    ● etc...

    View Slide

  8. Solution (we need)

    View Slide

  9. Basic aproach
    Parser
    Transformer
    TS Compiler
    1. Parse input and tokenize into JSON
    2. Transform tokens to TypeScript AST
    3. Validate AST and emit useful code
    Outcome

    View Slide

  10. View Slide

  11. Working Projects

    View Slide

  12. ① Typing Database Schema (db-recon)
    ● db-recon ( https://github.com/andoshin11/db-recon )
    ● Generates type-safe db client for Node.js application
    ● Supports MySQL and MSSQL currently

    View Slide

  13. ① Typing Database Schema (db-recon)

    View Slide

  14. ① Typing Database Schema (db-recon)

    View Slide

  15. ① Typing Database Schema (db-recon)

    View Slide

  16. ① Typing Database Schema (db-recon)
    TODO:
    ● Create SQL-to-AST parser
    ● Get semantic diagnostics for raw SQL literal string
    ( ts-sql-plugin requires DSL )
    ● Rewrite ejs templates with TS Compiler API

    View Slide

  17. ② Typing API Request/Response (squelette)
    ● squelette ( https://github.com/andoshin11/squelette )
    ● Generates type definitions for API request/response from
    Open API spec
    ● Toolchain to build your own type-safe API client

    View Slide

  18. ② Typing API Request/Response (squelette)

    View Slide

  19. ② Typing API Request/Response (squelette)

    View Slide

  20. ② Typing API Request/Response (squelette)

    View Slide

  21. ② Typing API Request/Response (squelette)

    View Slide

  22. ② Typing API Request/Response (squelette)
    TODO:
    ● Rewrite remaining ejs templates with TS Compiler API
    ● Improve parser and AST format

    View Slide

  23. Latest addition

    View Slide

  24. Improving DX for express.js

    View Slide

  25. Remaining Pains...

    View Slide

  26. Remaining Pains...
    need manual mapping
    need to define types
    may contain typo

    View Slide

  27. Leads to fatal error in future...

    View Slide

  28. What if...

    View Slide

  29. All handlers are typed like a magic ‍♂

    View Slide

  30. View Slide

  31. Introducing...

    View Slide

  32. typed-oax

    View Slide

  33. Generate express.d.ts from Open API

    View Slide

  34. Generate express.d.ts from Open API

    View Slide

  35. Generate express.d.ts from Open API

    View Slide

  36. DEMO

    View Slide

  37. typed-oax
    TODO:
    ● Separate handler types by HTTP Method
    ● Wrap with TS Plugin?

    View Slide