Custom, reusable type definitions

I want to be able to define custom, reusable type defintions to avoid error prone reusable definitions inside functions.

Based on the following error:

Error:

cause: Type `String` is not a subtype of `"Github" | "Facebook" | "Passkey" | "Google"`
 ›   at src/lib/db/schema/fsl/functions/auth/signUpWithSocialProvider.fsl:2:112
 ›     |
 ›   2 | function signUpWithSocialProvider(firstName: String, lastName: String, email: String, image: String, provider: String, providerAccountId: String): Account {
 ›     |                                                                                                                ^^^^^^
 ›     |

Collection account:

collection Account {
    user: Ref<User>
    provider: "Github" | "Google" | "Facebook" | "Passkey"
    providerAccountId: String | Null

    unique [.user, .provider]
}

If String is not a subtype of a string literal union, I need to add the string literal type directly. Any best practices? Because as far as I know it’s not yet possible to create custom types to reuse types e.g.

type Provider: "Github" | "Facebook" | "Passkey" | "Google"

function signUpWithSocialProvider([...], provider: Provider): Account

And duplicating the string literal union is quite error prone

function signUpWithSocialProvider([...], provider: "Github" | "Facebook" | "Passkey" | "Google"): Account

You are correct that you will need to provide the more specific type directly in your type signature. We don’t have the syntax to declare custom, reusable types, yet.

This is a good example of where custom types would indeed be helpful. Keeps things DRY and, as you say, less prone to errors. We don’t already have a FeatureRequest topic for this, so I will convert this over to track and continue discussion there.

1 Like