Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
Obtenga el tipo de una clase anidada en TypeScript

Estoy usando clases anidadas en TypeScript usando el siguiente código:

 class Parent { private secret = 'this is secret' static Child = class { public readSecret(parent: Parent) { return parent.secret } } }

Esto se basa en la siguiente respuesta . Permite que mi clase Child acceda a las propiedades privadas de la clase Parent .

Quiero escribir una variable con el tipo de clase anidada, comencé ingenuamente con el siguiente código:

 type NestedType = Parent.Child const child: NestedType = new Parent.Child()

Pero me sale el siguiente error:

 'Parent' only refers to a type, but is being used as a namespace here. ts(2702)

Intenté usar el operador typeof :

 type NestedType = typeof Parent.Child const child: NestedType = new Parent.Child()

Pero no funciona:

 Property 'prototype' is missing in type '(Anonymous class)' but required in type 'typeof (Anonymous class)'. ts(2741)

Finalmente pude hacerlo funcionar:

 const nestedTemplate = new Parent.Child() type NestedType = typeof nestedTemplate const child: NestedType = new Parent.Child()

Sin embargo, este código crea una instancia inútil de Child . ¿Es esta una limitación del lenguaje TS?

FYI, también probé usando espacios de nombres:

 class Parent { private secret = 'this is secret' } namespace Parent { export class Child { public readSecret(parent: Parent) { return parent.secret } } }

Pero entonces la clase Child ya no puede acceder a las propiedades privadas de la clase Parent :

 Property 'secret' is private and only accessible within class 'Parent'. ts(2341)
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Usar el prototipo typeof child debería ser suficiente para resolver sus problemas de tipeo.

 class Parent { private secret = 'this is secret' static Child = class { public readSecret(parent: Parent) { return parent.secret } } } const child: typeof Parent.Child.prototype = new Parent.Child(); console.log(child.readSecret(new Parent()));

Puedes probarlo allí Playground Link

De esa manera, no tiene que instanciar ninguna clase intermedia.

about 4 years ago · Juan Pablo Isaza Report

0

Puede acceder a la clase Child a través de la notación de acceso a la propiedad de la interfaz ( foo["bar"] ):

 class Parent { private secret = 'this is secret' static Child = class { public readSecret(parent: Parent) { return parent.secret } } } type ParentChildProto = (typeof Parent)["Child"]; const child: InstanceType<ParentChildProto> = null!; // same as assigning to `new Parent.Child()` child.readSecret(new Parent());

Vínculo de juegos de TypeScript

Editar: use el tipo de InstanceType incorporado, vea el comentario.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!