Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

272
Vistas
TypeScript: "Pick" child definitions of a picked definition

I have the following interface:

export interface WithQueryBuilder {
  queryBuilderProps: {
    columns: { [key: string]: { enabled: boolean; label: string; key: string } };
    meta: Record<string, unknown>;
    filters: Record<string, unknown>;
    search: {
      [key: string]: { enabled?: boolean; key: string; label: string; value: null | string };
    };
    page: number;
    sort: null;
  };
}

I now wanna create a new interface that consists of the properties in queryBuilderProps and some additional ones. I tried the following:

interface TableProps extends Pick<WithQueryBuilder, 'queryBuilderProps'> {
  onUpdate?: () => void;
}

What I expected is a type like that:

{
   onUpdate: ...,
   columns: ...,
   filters: ...,
   search: ...,
   [...]
}

Instead the type looks like this:

{
   onUpdate: ...,
   withQueryBuilder: { columns, meta, filters, ...),
}

I understand why this happens, but I wonder if there is a way to "pick" all the child definitions instead of the queryBuilderProps as a whole. Kinda like a spread operator for TypeScript.

Does anything like that exist?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

We can use an indexed access type to look up a specific property on another type:

type Person = { age: number; name: string; alive: boolean }; type Age = Person["age"]; type Age = number

TypeScript Handbook: Indexed Access Types

You may still use interface for your TableProps as follow:

export interface WithQueryBuilder {
  queryBuilderProps: {
    columns: { [key: string]: { enabled: boolean; label: string; key: string } };
    meta: Record<string, unknown>;
    filters: Record<string, unknown>;
    search: {
      [key: string]: { enabled?: boolean; key: string; label: string; value: null | string };
    };
    page: number;
    sort: null;
  };
}

// Here is the important part, this is the correct way to get the sub-type
type QueryBuilderProps = WithQueryBuilder['queryBuilderProps']

// You can still use interface here
interface TableProps extends QueryBuilderProps {
  onUpdate?: () => void;
}

const obj: TableProps = {
    onUpdate() {},
    columns: {
        "key": { enabled: true, label: `label`, key: `key` },
    },
    meta: {},
    filters: {},
    search: {},
    page: 1,
    sort: null,
}

// The following will throw error:
//   An interface can only extend an identifier/qualified-name with optional type arguments.(2499)
interface TableProps2 extends WithQueryBuilder['queryBuilderProps'] {
  onUpdate?: () => void;
}

TS Playground

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can access a sub-property of an Interface by using square brackets []:

type TableProps = WithQueryBuilder["queryBuilderProps"] & {
  onUpdate?: () => void
}

Sandbox

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda