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.
Getting started
Section titled “Getting started”- Intial setup
Follow the steps for setting up TypeScript in your app in @soleil-se/build-app.
- Update entry points
Change the file extensions of the entry points in your config to
.ts.config/config.js→config/config.tsconfig/index.js→config/index.tsconfig_global/config.js→config_global/config.tsconfig_global/index.js→config_global/index.ts
- Start build script
Start the build script as usual.
Terminal window npm run start
Saved values
Section titled “Saved values”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.
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 }));<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.
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);});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 }));<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>Validation
Section titled “Validation”The package exports a validator type for each component that supports custom validation. Import these types from @soleil-se/config-svelte.
| Validator type | Component |
|---|---|
CheckboxValidator | Checkbox |
CheckboxGroupValidator | CheckboxGroup |
CustomListValidator | CustomList |
CustomSelectorValidator | CustomSelector |
DropdownSelectorValidator | DropdownSelector and its specialized selectors |
InputValidator | Input |
ListSelectorValidator | ListSelector and its specialized list components |
NodeSelectorValidator | NodeSelector and its specialized selectors |
NumberSpinnerValidator | NumberSpinner |
RadioGroupValidator | RadioGroup |
SelectValidator | Select |
TagSelectorValidator | TagSelector |
TextareaValidator | Textarea |
TextListValidator | TextList |
NodeSelectorValidator, DropdownSelectorValidator, and ListSelectorValidator are shared by their respective base components and specialized wrappers.
Component validator type
Section titled “Component validator type”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>Explicit context types
Section titled “Explicit context types”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>