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 cover the basics of routing in Phoenix. https://elixircasts.io/phoenix-routes
New
New
remzolotykh
Hi all. I’m new here and kinda new to Elixir and Phoenix, but I love it so much. These days PETAL stack is my favourite way to build apps...
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
AlchemistCamp
https://alchemist.camp/episodes/guessing-game I’ve recently re-recorded Lesson 1 from Alchemist Camp. It’s significantly higher quality ...
New
New
Crowdhailer
Tokumei helps with building cloud native web applications. Some of the features that help to make it cloud ready. Containerised by def...
New
anuragg
One of our users just created this screencast for deploy Elixir apps on Render. Hope it’s helpful!
New
PJUllrich
For 2021, I decided to start a YouTube channel about coding with Elixir! Hurray! I learn best by teaching others, which is why I “challe...
New
sadjow
Ben Awad posted a youtube video about a project he created with Phoenix LiveView:
New

Other popular topics Top

JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
William
I would like to know that is there any online source for learning Phoenix Framework for building E-Commerce Store? Any advantage on build...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
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
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
beno
I will often find my self writing things similar to: case some_value do nil -&gt; something() "" -&gt; something() _ -&gt; someth...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement