sevensidedmarble

sevensidedmarble

Since static typing is all the rage in Elixir right now, maybe some of you will appreciate my most recent video. It’s all about how to get started with TypeScript in your Phoenix apps (assuming you’re using the default esbuild).

Let me know if you have any suggestions for further videos!

Showing Posts 1 to 3

arcanemachine

arcanemachine

Nice work. I love how easy Phoenix makes it to get started with TS. Literally just rename your *.js files to *.ts and ESBuild will pick them up automagically.

You brought up typosquatting when you searched for the LiveView types on npm. That’s something that should be emphasized very strongly. npm is rife with malware these days, and there are a lot of packages that are waiting for people to install something with the wrong name. I always copy and paste the package name from npm, I never type it. Yes, this is probably overkill, but I don’t want to slip up and shoot myself in the foot.

With the Property 'liveSocket' does not exist on type 'Window...' bit, I haven’t used TS in a bit and I am a bit of a noob on that subject, but I always just worked around that with window["liveSocket"] = liveSocket;. Not sure if that is even supposed to work, but it always did the trick for me.

Again, good stuff.

sevensidedmarble

sevensidedmarble OP

Yeah you have to be really careful with npm install for sure.

And the big advantage of properly augmenting the window type is that then other parts of your code will know that thing exists on window.

francois-codes

francois-codes

Coming from a front end dev background, I’m really happy to see a move towards typescript on Elixir :slight_smile:

the video is great indeed, but I was a bit annoyed that we have to use casts, satisfies, etc, to correctly type hooks. So I pushed this PR on the type package: fix(phoenix-live-view): improve phoenix liveView hooks types by francois-codes · Pull Request #71190 · DefinitelyTyped/DefinitelyTyped · GitHub

I initially thought I’d offer to add these type declarations inside the phoenix framework repo, but promptly came into an issue about this where Chris closed it saying “I have no interest about that” :grimacing: so I guess it will have to live in definitely typed for now

regarding your point @arcanemachine , you need to extend the window interface like this

declare global {
  interface Window {
    liveSocket: LiveSocket
  }
}

either include this in your app.ts file, or better, in a .d.ts file referenced in the types array of your tsconfig

This is also where you can type custom phoenix events. let’s say you’re calling push_event("custom-event", args) on the elixir side, you’d probably define listeners on your ts code with

document.addEventListener("phx:custom-event", ({detail }) => { ... })

this will raise a type error because the event is not known to typescript. but you can use the same approach above and extend the WindowEventMap interface

interface EventPayload { ... }

declare global {
  interface WindowEventMap {
    'custom-event': CustomEvent<EventPayload>
  }
}

this way the argument of your event handler function will be correctly typed

— All posts loaded —

Where Next? Top

Trending in Screencasts Top

lubien
Several weeks ago I was nerd snipped by browsers support for View Transitions API and took to myself to see how that goes in LiveView. Af...
New
Potions
Sooner or later you’ll need to run a one-off or recurring task on a deployed app, and if you deploy with releases the usual answer, a Mix...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews