A Svelte TypeScript component must have a minimum of one prop passed to it

I am interested in developing a unique svelte component that necessitates at least one prop to be provided.

Consider a component with two exposed props, a and b:

export let a: number | undefined;
export let b: number | undefined;

It should be permissible to pass either just a or just b:

<Component a={1} />
<Component b={1} />

However, passing neither should not be allowed:

<Component />

The overall type would resemble something along these lines:

type Component = {
    a: number
} | {
    b: number
}

Unfortunately, I am unsure of how to communicate this type information to Svelte.

Answer №1

It appears that you are referring to this

interface ConfigurationA {
  x: number;
  y?: never;
}

interface ConfigurationB {
  x?: never;
  y: number;
}

type Configuration = ConfigurationA | ConfigurationB;

const config1: Configuration = {
  x: 10,
};

const config2: Configuration = {
  y: 20,
};

Answer №2

One approach is to define property types using $$Props, but this method attempts to match the types with those specified on the exports, which may result in errors depending on the TypeScript configuration.

For example:

<script lang="ts">
    type $$Props =
        { a: number, b?: never } |
        { b: number, a?: never }

    export let a: number | undefined
    export let b: number | undefined
</script>

This could potentially work for your specific situation.

Check out the RFC for this feature (feel free to provide feedback here)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to find the 'Restore Packages' option when right-clicking on package.json in Visual Studio 2015 update3

Currently embarking on my Angular 5 journey with Visual Studio 2015 (Update 3), I encountered an issue where the "restore packages" option is missing when right-clicking on the 'packages.json' file. Any advice on how to proceed? ...

Retrieving the component's values when utilizing the `<ng-content>` directive

Seeking a technique for accessing the values of a component when utilizing <ng-content>: import {Component} from '@angular/core'; @Component({ selector: 'home-page', template: `<person-box>{{name}}</person-box> & ...

Ways to indicate in Typescript that a value, if it exists, is not undefined

Is there a way to represent the following logic in TypeScript? type LanguageName = "javascript" | "typescript" | "java" | "csharp" type LanguageToWasmMap = { [key in LanguageName]: Exclude<LanguageName, key> ...

Emphasize a Row Based on a Certain Criteria

One of the challenges I am facing is how to emphasize a specific row in a table based on certain conditions. Currently, I am utilizing Jqxgrid and have made some modifications in the front-end to achieve the highlighting effect: TypeScript: carsDataAgain ...

Tips for customizing the appearance of the initial React component frame

While working on my app's loading screen, I noticed a brief moment when a blank white page appears. Even the default React app displays this issue, but interestingly, it does not occur on platforms like Stack Overflow. This wouldn't be a concern ...

The 'fullDocument' property is not present in the 'ChangeStreamDropDocument' type

Upon cloning an express TypeScript project, I encountered a Typescript error within a Mongo related function as mentioned in the title. Property 'fullDocument' does not exist on type 'ChangeStreamDocument<IUser>'. Property &apos ...

Angular back-end data filtering tutorial

I have a user input field where users can search or type in data, but I want to restrict them to only searching for predefined data that is available from the backend. I do not want users to be able to create new data by typing something that is not alread ...

Tips for leveraging constant values in TypeScript 'Type Aliases' for improved code organization

I am a beginner in the world of TypeScript. Currently, I am working on an Angular Project where I am developing a SnackBar Service to provide notifications to users. Although my background is primarily in Java, I have encountered some challenges. I have ...

Storing my token in the cookie using setCookie is proving to be difficult for me

While trying to authenticate my site with nextAuth and nookies, I am facing an issue where the token is not getting stored in the cookie. The API sends back a token and a refresh, and even though storing the refresh works fine, the token just doesn't ...

The process of declaring a nullable callback variable in TypeScript

Can the declaration of the resolve variable in the TypeScript code below be simplified while maintaining type safety? I am new to TypeScript, so please bear with me. The objective is to store the promise resolver callback that is passed to the executor fu ...

Overhauling JSON Data Output in Angular Version 2 and beyond

Currently, I am dealing with a complex web API JSON response that contains nested data. I would like to simplify the structure by extracting only the necessary information. How can I achieve this using Angular 2+/Typescript? I would greatly appreciate an ...

Exploring the wonders of Angular 2: Leveraging NgbModal for transclusion within

If I have a modal template structured like this: <div class="modal-header"> <h3 [innerHtml]="header"></h3> </div> <div class="modal-body"> <ng-content></ng-content> </div> <div class="modal-footer"& ...

Managing state within SolidJS components using Immer's "produce" for nested state handling

I've been working on a SolidJS application where I store a large JSON object with nested objects. For undo and redo actions, I'm using Immer to generate patches. Although technically I'm storing a class with multiple layers of nesting, Immer ...

Issue encountered when attempting to access disk JSON data: 404 error code detected

I am attempting to retrieve JSON data from the disk using a service: import { Product } from './../models/Product'; import { Injectable } from '@angular/core'; import { Observable } from 'rxjs'; import { HttpClient } from &apo ...

IntelliSense or Closure Compiler cannot locate the TypeScript class

Consider the following TypeScript code snippet: class TestClass { constructor() { } execute(): void { } } This TypeScript code gets transpiled to the following JavaScript code: var TestClass = /** @class */ (function () { function TestClass() ...

Creating a new instance of a class using a JSON object in Angular and TypeScript

In handling responses from my Web API, I have designed an interface: interface Person { dateofbirth: string; firstname: string; lastname: string; } However, I prefer to work with a MomentJS object rather than a string representation of a date-time. ...

Error message: In Angular 2, when utilizing ng2-interceptor, the type 'ServerURLInterceptor' does not have a property called 'post'

I encountered an error while using angular2-interceptors in my Angular2 project. When attempting to call a POST/GET method, the following message appeared: 'Property 'post' does not exist on type 'ServerURLInterceptor'.' I n ...

What sets apart the utilization of add versus finalize in rxjs?

It appears that both of these code snippets achieve the same outcome: Add this.test$.pipe(take(1)).subscribe().add(() => console.log('added')); Finalize this.test$.pipe(take(1), finalize(() => console.log('finalized'))).sub ...

What triggers the execution of the catch method while chaining HTTP promises?

In my Angular application, I have developed a custom loaderService that displays a loading spinner while the back-end processes requests. Below is the code snippet: export class ComponentOne { constructor(loaderService: LoaderService, orderService: Orde ...

Issue: In an Ionic-Angular application, argon2-browser modules are not being found

I am attempting to integrate argon2-browser into my Ionic-Angular project. Upon syncing with ionic cap sync I encounter the following errors: ./node_modules/argon2-browser/dist/argon2.js:35:22-45 - Error: Module not found: Error: Can't resolve ' ...