16778

How to reference instance of factory created class in Typescript? Cannot find name error

Question:

Typescript is failing to reference factory created class. Here's an example code:

// factory.ts export const createClass = () => class Model { // ... } // ModelA.ts import {createClass} from './factory'; export const ModelA = createClass(); let a: ModelA; // Cannot find name 'ModelA'; // other.ts import {ModelA} from './ModelA'; new ModelA() // Cannot find name 'ModelA';

What am I doing wrong here?

Answer1:

If you are trying to get the insurance type for the class generated by the function then you can use the InstanceType conditional type.

export const createClass = () => class Model { // ... } export const ModelA = createClass(); type ModelA = InstanceType<typeof ModelA> let a: ModelA = new ModelA(); // ok

Answer2:

Try this in ModelA.ts

export const ModelA = createClass(); export type ModelA = typeof ModelA;

Edit (credits for Joon), for future readers

The solution above apparently doesn't work as you're unable to

const foo: ModelA = new ModelA() //Type 'Model' is not assignable to type 'typeof Model'. Property 'prototype' is missing in type 'Model'.

One proposed in comment works

export type ModelA = typeof ModelA['prototype'];

(not sure why though).

Recommend

  • How can I set a DateField format in django from the model?
  • TTTAttributedLabel Delegate didSelectLinkWithURL not getting called
  • has-many-through create! validate presence
  • Meteor app ran with pm2 FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScript heap out of
  • how to replace the current launch image?
  • Angular 2 router auxiliary routes not working on redirectTo
  • Deploying pre-encrypted configuration files to a production environment
  • Instanciate service on startup in Angular2
  • get passwords from chrome
  • Table striping rows in CSS Grid
  • Fragment gives me an error while inflating 1
  • Select inner HTML item in CSS
  • Is there a way to set up a fallback for the formAction attribute in HTML5?
  • How to upload specific List image using click on Upload button
  • Pass array from parent template to child template
  • HttpClient: disabling chunked encoding
  • How can I run DataNucleus Bytecode Enhancer from SBT?
  • Angular2 component view does not update on value change via method
  • Typescript - Unable to get 'import' statement to function
  • How to use an array of arrays with array_map(…) in PHP?
  • Why is an OPTIONS request sent to the server?
  • How to set/get protobuf's extension field in Go?
  • Cassandra Data Model
  • Where to put my custom functions in Wordpress?
  • Trying to switch camera back to front but getting exception
  • Importing jscolor library in angular 2
  • RestKit - RKRequestDelegate does not exist
  • WPF Applying a trigger on binding failure
  • How can I remove ASP.NET Designer.cs files?
  • python draw pie shapes with colour filled
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Is there any way to bind data to data.frame by some index?
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How can i traverse a binary tree from right to left in java?
  • How can I use `wmic` in a Windows PE script?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?