Create a new interface subtype for a component

Is it possible to incorporate an "interface subtype" into a component, such as this:

@Component
export default class Graph extends Vue
{

    @Prop ()
    public data: Array<Graph.Data> ;

}

which includes a sub-interface like:

export interface Data
{
    time: Date
    value: number
}

to ensure that the caller provides correctly formatted props:

<graph :data="data" />

where data must be of type Array<Graph.Data>.

Any suggestions or thoughts on how to achieve this?

Answer №1

Even though TypeScript does not directly support inner interfaces, you can utilize namespace/module to achieve the desired outcome:

@Component
class Graph extends Vue {

    @Prop()
    public data: Array<Graph.Data>;

}
namespace Graph {
    export interface Data  {
        time: Date;
        value: number;
    };
}
export default Graph;

// Sample variable for demonstration
var someData: Array<Graph.Data> = [];

For a detailed showcase, check out this TypeScript playground demo.

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

Utilizing Vue.js with Electron: Establishing seamless data synchronization between backend and frontend

I am currently developing a Vue-Electron application. Picture a settings page on the frontend where users can adjust various variables. It is crucial for me to ensure that all settings are saved even when the settings page is reloaded. To achieve this, I r ...

Converting a nested JSON object into a specific structure using TypeScript

In the process of developing a React app with TypeScript, I encountered a challenging task involving formatting a complex nested JSON object into a specific structure. const data = { "aaa": { "aa": { "xxx&qu ...

Sending data to child component in Angular 2

Is there a way to seamlessly pass attributes from wrapper component to nested component? When we have const FIRST_PARTY_OWN_INPUTS = [...]; const FIRST_PARTY_PASSTHROUGH_INPUTS = ['all', 'attrs', 'are', 'passed']; ...

Tips on invoking a child component's function from the parent component

I'm struggling to comprehend, Is there a way to trigger a function within a child component by clicking on a button in the parent component? ...

Is there a way to initiate validations for inputs within ReactiveForms?

After creating a form using Reactive Forms, I have implemented functionality for users to set a new password. The process involves entering both the old and new passwords, as well as confirming the new password. Throughout development, I considered the fol ...

Determine the type of connection using knex

I am currently developing an application that needs to support both MySQL and Postgres databases simultaneously through Knex. However, I have encountered a limitation with the query builder for certain features, which requires me to write specific queries ...

The structure of a project using a Vue app and a Node API

Seeking your thoughts on combining a Vue App with a Node using API for my upcoming project. I have set up two separate folders for the client (VueJs) and server (Node) locally: - client (VueJs) - server (Node) I am currently running them individually usi ...

Exploring Typescript Reflection: The Importance of Required Parameters and Default Values

In summary: Is there a method to determine whether a typescript parameter is mandatory and/or has a preset value? Expanding further: Imagine I have the code snippet below: //Foo.ts class Bar { foo(required:string,defaultValue:number=0,optional?:boole ...

Loading modules conditionally in Nuxt.js

In my Nuxt.js configuration, I have included a module for Google Tag Manager like this: modules: [ [ '@nuxtjs/google-tag-manager', { id: 'GTM-XXXXXXX' } ] ] Everything is functioning properly, but I am curious ab ...

switching the content of a button when it is clicked

I am currently using Angular 7 and I am trying to achieve a functionality where the text on a button changes every time it is clicked, toggling between 'login' and 'logout'. Below is the code snippet I have been working on: typescript ...

Ways to incorporate StropheJs into Angular 8

Methods I have attempted: Method 1: Downloaded the strophejs-1.3.4.zip from Unzipped the folder, strophejs-1.3.4, and placed it in the src/assets directory of my Angular 8 project. Included the following in my index.html file: <!--// Stroph ...

What is the best way to customize a React component using TypeScript?

Let's say I have a functional component like this: function MyComp({a, b, c, d, e, f}: any) { ... } Is there a way to create a specialized version of this component where I provide values for "a" and "b," but allow other users to choose the r ...

Creating XML templates in Angular 7: A comprehensive guide

How do I pass XML values in Angular 7 when the API requires this specific format of XML code? -modifydata "<datasets><dataset select=\""always\""> <replace match=\""Letter/@FName\"" value=\""Nazeeeeeeeeeeeeer\" ...

Utilizing a Typescript class interface does not maintain the original method types

Struggling to define a Typescript interface and implement it in a class. The issue lies in the method signatures of the interface not being applied to the class as expected. Below is a simplified example: export interface Foo { bar(value: string): voi ...

Creating a new array in Vue.js by filtering the results of a promise iteration

Is there a way to use the splice method to insert promise values from an old array into a new one for vue reactivity? I'm encountering an issue where the newArray remains empty and does not receive any values. Check out this link for more information. ...

Tips for integrating error management in Angular with RXJS

Implementing error handling in the use case method by using subscriptions is my goal. When an error occurs in the adapter, the handling should be done in the use case. However, in the code snippet provided below, the catch block does not seem to work pro ...

Displaying only the validation messages that are accurate according to the Vuetify rules

<v-text-field label='New Password' class="required" v-model='password' type='password' :rules="passwordRules" required> </v-text-field> passwordRules: [ value => !!value || 'Pl ...

Facing challenges with incorporating Prisma Schema into a PNPM monorepo alongside NestJS due to ESM complications

I've set up a PNPM monorepo containing two identical NestJS apps (apps/api and apps/backend) and one Prisma database package (packages/database). When trying to run the api using start:dev, I encountered the following error message: (node:69498) Warni ...

Effortless implementation of list loading with images and text in the Ionic 2 framework

Can someone provide guidance on creating a lazy loading list with both images and text? I understand that each image in the list will require a separate http request to download from the server. Should caching be implemented for these image downloads? Addi ...

How to add a service to a static function in Angular

After incorporating a logger service into my project, I have encountered an issue with using it in NGXS static selectors. The selectors in NGXS are static methods, which prevent me from accessing the logger service injected via Angular DI. Are there any e ...