sevensidedmarble

sevensidedmarble

Getting started with TypeScript in your Phoenix projects!

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!

Most Liked

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

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

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.

Where Next?

Popular in Screencasts Top

ElixirCasts
I made a video to help people get introduced to agents.
New
aseigo
Unfortunately, I missed last week’s schedule as I had a pair of subjects related to the topic of debugging and testing .. but ended up de...
New
New
ElixirCasts
I made an episode for anyone wanting an introduction to testing with Elixir. https://elixircasts.io/introduction-to-testing
New
ElixirCasts
Made a video about introspecting Ecto Schemas. Posted via Devtalk (see this thread for details).
New
ElixirCasts
I did an episode on Phoenix contexts. This is a good episode for anyone new to Phoenix or wanting to see how contexts work. https://elix...
New
sevensidedmarble
New video dropped: this time on manually running your own asset build script with Bun. Bun is used as the script runner with ESBuild stil...
New
CinderellaMan
Hands-on Elixir &amp; OTP: Create a cryptocurrency trading bot - Ep. 16 - Add end-to-end tests elixir | otp | learning | screencast | cry...
New
zacksiri
At Codemy.net we are always trying to find solutions for problems in a way that works well. As a result we’ve been able to create a beaut...
New
mplatts
Elixir &amp; Phoenix Testing 101 Hi guys, I’d like to get into creating more educational videos focuses on building web apps with Phoenix...
New

Other popular topics Top

aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43757 214
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39467 209
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement