hoshi
hoshi
TTCTheo's Typesafe Cult
Created by hoshi on 8/3/2024 in #questions
How to test nextjs app against dockerized postgres container (Vercel)
Also open to using Github actions. Something like this seems promising https://gist.github.com/2color/537f8ef13ecec80059abb007839a6878
4 replies
TTCTheo's Typesafe Cult
Created by hoshi on 9/30/2022 in #questions
Typescript Function to make specific nested keys non nullable?
Wow thanks, really appreciate breaking it down for me like that. I think I understand it a lot more now. I was not expecting never to extend everything else but now that I think about it, it makes sense given that never is an empty set. So of course an empty set is a subset of all other sets. I actually wanted GetUniquePaths to filter out the most "shallow" path and retain the deeper paths. So I wanted this output:
type t1 = GetUniquePaths<"a.c" | "a" | "c"> // => "a.c" | "c"
type t1 = GetUniquePaths<"a.c" | "a" | "c"> // => "a.c" | "c"
Thanks to your explanation, I ended up implementing it like this:
type IsSubPath<Paths extends string, P extends string> = Paths extends unknown
? Paths extends `${P}.${string}`
? true
: false
: never

type GetUniquePaths<T extends string, P extends string = T> = P extends P
? IsSubPath<T, P> extends false
? P
: never
: never


type t1 = GetUniquePaths<"a" | "a.b" | "c" | "c.a" | "a.b.c" | "c.d"> // "c.a" | "a.b.c" | "c.d"
type IsSubPath<Paths extends string, P extends string> = Paths extends unknown
? Paths extends `${P}.${string}`
? true
: false
: never

type GetUniquePaths<T extends string, P extends string = T> = P extends P
? IsSubPath<T, P> extends false
? P
: never
: never


type t1 = GetUniquePaths<"a" | "a.b" | "c" | "c.a" | "a.b.c" | "c.d"> // "c.a" | "a.b.c" | "c.d"
13 replies
TTCTheo's Typesafe Cult
Created by hoshi on 9/30/2022 in #questions
Typescript Function to make specific nested keys non nullable?
Trying to come up with a typescript function to filter out "subpaths" now. It seems like it should be easy but I'm getting stuck with the peculiarities with how unions are distributed in a condition.
type FilterForA<T extends string, P = T> = P extends P
? P extends "a" ? P : never : never

type FilterForAV2<T extends string, P = T> = P extends P
? Exclude<P, "c"> extends "a" ? P : never : never

type t1 = FilterForA<"a.c" | "a" | "c"> // gives "a"
type t2 = FilterForAV2<"a.c" | "a" | "c"> // gives "c" | "a" for some reason
type FilterForA<T extends string, P = T> = P extends P
? P extends "a" ? P : never : never

type FilterForAV2<T extends string, P = T> = P extends P
? Exclude<P, "c"> extends "a" ? P : never : never

type t1 = FilterForA<"a.c" | "a" | "c"> // gives "a"
type t2 = FilterForAV2<"a.c" | "a" | "c"> // gives "c" | "a" for some reason
13 replies
TTCTheo's Typesafe Cult
Created by hoshi on 9/30/2022 in #questions
Typescript Function to make specific nested keys non nullable?
13 replies
TTCTheo's Typesafe Cult
Created by hoshi on 9/30/2022 in #questions
Typescript Function to make specific nested keys non nullable?
13 replies