Skip to content

Instantly share code, notes, and snippets.

@m93a
Last active March 11, 2024 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m93a/be8886d5ef28cab5e300cb23d84e1b76 to your computer and use it in GitHub Desktop.
Save m93a/be8886d5ef28cab5e300cb23d84e1b76 to your computer and use it in GitHub Desktop.

Clamped Value

Idealized syntax:

component ClampedValue {
  in min: number;
  in max: number;
  
  out value = clamp(min, prev value ?? 0, max);
}

TypeScript if decorators didn't suck:

@component
class ClampedValue {
  @input min: number;
  @input max: number;
  
  @computed value = ({ min, max }, prev) =>
    clamp(min, prev ?? 0, max);
}

Current TypeScript:

type ClampedValue = Component<typeof ClampedValue>;
const ClampedValue = component(
    class {
        min = input<number>();
        max = input<number>();

        value = computed<this, number>(
            ({ min, max }, prev) => clamp(min, prev ?? 0, max)
        );
    }
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment