Hoppa till innehåll
Markdown

TypeScript

The components and utils are fully typed with TypeScript. If you are using TypeScript in your app, you can take advantage of the type definitions to ensure type safety and better DX.

  1. Intial setup

    Follow the steps for setting up TypeScript in your app in @soleil-se/build-app.

  2. Update entry points

    Change the file extensions of the entry points in your config to .ts.

    • config/config.js → config/config.ts
    • config/index.js → config/index.ts
    • config_global/config.js → config_global/config.ts
    • config_global/index.js → config_global/index.ts
  3. Start build script

    Start the build script as usual.

    Terminal window
    npm run start

The saved values are passed to the component as the values prop. By default they are typed as Record<string, unknown>, but if you need to reference the values in your component, you should define a Props type and pass it to createConfigApp for better type safety.

config.ts
import { createConfigApp } from '@soleil-se/config-svelte';
import { mount } from 'svelte';
import App, { type Props } from './App.svelte';
createConfigApp<Props>(({ target, props }) => mount(App, { target, props }));
App.svelte
<script lang="ts" module>
export type Props = {
values?: {
text?: string;
};
};
</script>
<script lang="ts">
import { Panel, Input } from '@soleil-se/config-svelte';
let { values }: Props = $props();
console.log(values.text);
</script>
<Panel>
<Input name="text" label="Text" />
</Panel>

If you pass additional props from the server to the config app, you can also type those by adding them to the Props type.

index.ts
import router from '@sitevision/api/common/router';
import { createAppProps } from '@soleil-se/config-svelte/server';
import type { Props } from './App.svelte';
router.get('/', (req, res) => {
const props: Props = {
foo: 'bar',
fizz: 'buzz',
};
createAppProps(props, res);
});
config.ts
import { createConfigApp } from '@soleil-se/config-svelte';
import { mount } from 'svelte';
import App, { type Props } from './App.svelte';
createConfigApp<Props>(({ target, props }) => mount(App, { target, props }));
App.svelte
<script lang="ts" module>
export type Props = {
foo: string;
fizz: string;
};
</script>
<script lang="ts">
import { Panel, Input } from '@soleil-se/config-svelte';
let { foo, fizz }: Props = $props();
</script>
<p>Foo: {foo}</p>
<p>Fizz: {fizz}</p>
<Panel>
<Input name="text" label="Text" />
</Panel>

The package exports a validator type for each component that supports custom validation. Import these types from @soleil-se/config-svelte.

Validator typeComponent
CheckboxValidatorCheckbox
CheckboxGroupValidatorCheckboxGroup
CustomListValidatorCustomList
CustomSelectorValidatorCustomSelector
DropdownSelectorValidatorDropdownSelector and its specialized selectors
InputValidatorInput
ListSelectorValidatorListSelector and its specialized list components
NodeSelectorValidatorNodeSelector and its specialized selectors
NumberSpinnerValidatorNumberSpinner
RadioGroupValidatorRadioGroup
SelectValidatorSelect
TagSelectorValidatorTagSelector
TextareaValidatorTextarea
TextListValidatorTextList

NodeSelectorValidator, DropdownSelectorValidator, and ListSelectorValidator are shared by their respective base components and specialized wrappers.

Use the component-specific type to match the validator’s context to the component’s value type:

<script lang="ts">
import type { InputValidator } from '@soleil-se/config-svelte';
import { Panel, Input } from '@soleil-se/config-svelte';
const customValidate: InputValidator = (context) => {
const { value } = context;
if (!value) {
return { success: false, message: 'This field is required.' };
}
return { success: true };
};
</script>
<Panel>
<Input label="Example Input" validate={customValidate} />
</Panel>

Use ValidationContext<T> and ValidationResult directly when you need to control the context type or compose custom rules with validateWith. Input accepts string or numeric values, so its context type is ValidationContext<string | number>.

<script lang="ts">
import type { ValidationContext, ValidationResult } from '@soleil-se/config-svelte/utils';
import { Input, Panel } from '@soleil-se/config-svelte';
import { defaultValidator, validateWith } from '@soleil-se/config-svelte/utils';
function validateMinimumLength(context: ValidationContext<string | number>): ValidationResult {
if (typeof context.value === 'string' && context.value.trim().length < 5) {
return { success: false, message: 'Enter at least five characters.' };
}
return { success: true };
}
function validateTitle(context: ValidationContext<string | number>): ValidationResult {
return validateWith(context, defaultValidator, validateMinimumLength);
}
</script>
<Panel>
<Input name="title" label="Title" required validate={validateTitle} />
</Panel>