0

I am developing a react app with typescript. In the problem I am facing, there are 2 separate interfaces and one component class that these interfaces are imported. The problem is that when the app is run, the component class attempt to import both interfaces sequentially, however, the interfaces also imports each other which causes cyclic error-prone behavior and lead app to crash with TypeError: object prototype may only be an object or null: undefined. I put the sample code below. Moreover, I am not able to modify the data model which are fields within the interfaces. Any help are welcomed.

import {B} from "../../some/path";
import {C} from "../some/other/path";
export interface A {
    field1: B,
    field2: C
}

import {A} from "../../other/path";
import {D} from "../some/other/path";
export interface B {
     field1: A,
     field2: D
}

import {A} from "../../other/path"
import {B} from "../some/path"
export class TEST extend React.Component<>{
  this.inputEntity1: A;
  this.inputEntity2: B;
}

Note: All interfaces and the class are located different files.

Arda Tümay
  • 77
  • 1
  • 10
  • Why would the interfaces be importing one another? If they are separate they should not be doing that. That's just creating a circular dependency that cannot be resolved. Improve your component importing hierarchy to prevent this. – Jacob Penney Nov 26 '19 at 20:55
  • The reason is that one interface has a object whose type is of other interface and vice versa. How can I create interfaces that include other interfaces as a object then? – Arda Tümay Nov 27 '19 at 05:08
  • "one interface has a object whose type is of other interface and vice versa." That means you're using the interfaces in one another. You don't want to do that. Period. – Jacob Penney Nov 27 '19 at 05:20
  • What are you trying to accomplish by having objects import one another? They should be unaware of one another. Only the parent component should have a copy of them and control all the logic involved in displaying them – Jacob Penney Nov 27 '19 at 05:22
  • There is a hierarchy among the data model objects. It is like there are houses owned by person and each person has a house. And I need to create a data entry component for houses which accept house as a generic type and import both house and person interfaces on top of the class to cast data entered by user to person object and house object. – Arda Tümay Nov 27 '19 at 05:42
  • You should be able to instantiate a house without an owner and a person without a house. A person should be able to be assigned a reference id to a house or vice versa without needing access to one another's type. If they do, then you need to change your system. It's inherently flawed. – Jacob Penney Nov 27 '19 at 05:52
  • 1
    Ok. Thanks for comment. I try to fix flaws. – Arda Tümay Nov 27 '19 at 10:30

0 Answers0