Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypedStimulusController - some Typescript love :) #724

Closed
ajaishankar opened this issue Oct 8, 2023 · 6 comments
Closed

TypedStimulusController - some Typescript love :) #724

ajaishankar opened this issue Oct 8, 2023 · 6 comments

Comments

@ajaishankar
Copy link

ajaishankar commented Oct 8, 2023

Hi

Have been playing with Stimulus, and came up with this sample that can automatically type the values, targets, outlets...

Would there be any interest in including a full implementation of this as part of Stimulus or would it be more suited as a separate package?

type Statics<Values extends object> = {
    values?: Values
}

function TypedStimulusController<Values extends object>(statics: Statics<Values>) {
    return class extends StimulusController {
        static values = statics.values
        constructor(context: Context) {
            super(context)
        }
    } as unknown as {
        new (context: Context): StimulusController & TypedValues<Values>
    }
}

type TypedValues<Values extends object> = {
    [K in keyof Values as `${K & string}Value`]:
        Values[K] extends NumberConstructor ? number
        : Values[K] extends StringConstructor ? string
        : unknown
}

const values = {
    url: String,
    interval: Number
}

class LoaderController extends TypedStimulusController({ values }) {
    connect() {
        fetch(this.urlValue).then(/* … */)
    }
}

class Context {}

class StimulusController {
    constructor(context: Context) {}
}

This will let Typescript users avoid having to manually override the types

  declare urlValue: string
  declare readonly hasUrlValue: boolean

See similar issue here: #723

@mlwyatt
Copy link

mlwyatt commented Oct 9, 2023

I took a look at this and made some improvements (support default values) but was unable to make it work as well for targets and outlets

@brunoprietog
Copy link

TypeScript has already been removed from Turbo and I would not be surprised if it is removed from Stimulus soon

@ajaishankar
Copy link
Author

Removed as in...? Stimulus itself is written in Typescript.

@brunoprietog
Copy link

Yes, but it will probably switch to JavaScript soon, just like Turbo did a few weeks ago

@ajaishankar
Copy link
Author

Got it.

Even if the implementation switches to Javascript (for whatever reason), the type definitions would be published.

Anyway what I proposed is a nicety for the end typescript users, and can definitely live outside Stimulus as a separate package.

@ajaishankar
Copy link
Author

https://world.hey.com/dhh/turbo-8-is-dropping-typescript-70165c01

Ok, not part of Stimulus 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants