// `./module.ts` export class User { name: string surname: string age: number } 

es6 import

import * as module from "./module" generates a namespace module and this is bad.

  1. calling typeof module breaks class generics from module :

    type User is not assignable to parameter of type typeof User

  2. this module cannot be extended with the namespace module {} or interface module {} .

AT:

  • Is there a way to use namespace as a class in the context of generics ?
  • Is there a way to export multiple classes from a module as a class type without manual association?

 namespace _0.A - Ρ€Π΅Π°Π»ΡŒΠ½ΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ namespace _0.B - ΠΎΠΆΠΈΠ΄Π°Π΅ΠΌΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ namespace _1.extend - Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½ΠΈΠ΅ ΠΎΠΆΠΈΠ΄Π°Π΅ΠΌΠΎΠ΅ ΠΏΠΎΠ²Π΅Π΄Π΅Π½ΠΈΠ΅ 

see examples here typescriptlang.org/play or read on


 // import { User } from "./module" // import * as module from "./module" // Π°Π½Π°Π»ΠΎΠ³ΠΈΡ‡Π½ΠΎ этому: // (Π·Π° ΠΈΡΠΊΠ»ΡŽΡ‡Π΅Π½ΠΈΠ΅ΠΌ Ρ‚ΠΎΠ³ΠΎ, Ρ‡Ρ‚ΠΎ `namespace module` Π½Π΅ ΠΌΠΎΠΆΠ΅Ρ‚ Π±Ρ‹Ρ‚ΡŒ Ρ€Π°ΡΡˆΠΈΡ€Π΅Π½) class User { name: string surname: string age: number } namespace module { export class User { name: string surname: string age: number } } namespace _1 { let store = {} type store<T> = { [K in keyof T]: T[K][] } export function set<T>(k: keyof T, val: T[keyof T]) { if (!(store as store<T>)[k]) (store as store<T>)[k] = []; (store as store<T>)[k].push(val) } } namespace _1.A { type mo = typeof module; // -- set<mo>('User', new User) // [ts] Argument of type 'User' is not assignable to parameter of type 'typeof User'. } namespace _1.B { class mo { User: User } // -- set<mo>('User', new User) // ok } namespace _1.extend { class Admin { superpover: true } // -- class mo { User: User } class moExt extends mo { Admin: Admin } // -- set<moExt>('Admin', new Admin) } namespace _0.A { type m = typeof module; type user = m['User'] // bad // type user = typeof User let age: user['age'] // err // [ts] Property 'age' does not exist on type 'typeof User'. } namespace _0.B { class m { User: User } type user = m['User'] // good // type user = User let age: user['age'] // ok // } 

1 answer 1

decision

 type Convert<T> = { [k in keyof T]: T[k] extends { new(): infer R } ? R : T[k] } 

full code example, in the context of the question, see there typescriptlang.org/play