Soy nuevo en Typescript y tengo que crear una interfaz para el siguiente tipo de accesorios:
const myProps = [ { name: 'name1', on: true, children: [ { name: 'test1', href: '#' }, { name: 'test2', href: '#' }, { name: 'test3', href: '#' } ] }, { name: 'name2', on: false, children: [ { name: 'test1', href: '#' }, { name: 'test2', href: '#' }, { name: 'test3', href: '#' }, { name: 'test4', href: '#' }, { name: 'test5', href: '#' } ] }, { name: 'name3', on: false, children: [{ name: 'test1', href: '#' }] } ];Quiero crear una interfaz para que se use en una aplicación React + Typescript.
Esta es la interfaz hasta ahora:
export interface IChildren { name: string, href: string } export interface IMyProps { name: string, on: boolean, children: IChildren, }No está funcionando, debería tener algunas matrices, supongo. ¿Alguna sugerencia?
Puedes probar así,
export interface CommonProps { name: string; href: string; } export interface MyProps { name: string; on: boolean; children: Array<CommonProps>; }También tenga en cuenta que las interfaces de datos no deben comenzar con convenciones de nomenclatura
"I"Las interfaces que tienen declaraciones de métodos deben tener"I"comoIMethodService
Si está utilizando dos interfaces, puede utilizar los siguientes dos métodos.
export interface ChildrenProp { name: string, href: string } export interface MyProps { name: string; on: boolean; children: ChildrenProp[] }o
export interface MyProps { name: string; on: boolean; children: Array<ChildrenProp> }Si no está utilizando dos interfaces, puede utilizar el siguiente método
export interface MyProps { name: string; on: boolean; children: { name: string, href: string }[] }