S
S
Svyatoslav Khusamov2017-07-13 18:05:14
typescript
Svyatoslav Khusamov, 2017-07-13 18:05:14

As soon as TS-classes are in different files, errors appear. How to fix it?

If everything is in one file, then there are no errors. But as soon as I spread them into different files, the following errors appear:

/index2.ts(13,13): error TS2345: Argument of type '{ name: string; value: number; }' is not assignable to parameter of type 'ConfigOption | ConfigOption[]'.
Type '{ name: string; value: number; }' is not assignable to type 'ConfigOption[]'.
Property 'length' is missing in type '{ name: string; value: number; }'.

/index2.ts(19,12): error TS2345: Argument of type '{ name: string; value: string; }[]' is not assignable to parameter of type 'ConfigOption[]'.
Type '{ name: string; value: string; }' is not assignable to type 'ConfigOption'.
Property 'comment' is missing in type '{ name: string; value: string; }'.
17:58:23 Compilation complete. Watching for file changes.

Here is the code in one file:
interface IOption {
  name: string;
  value?: any;
  comment?: string;
}

class ConfigOption {
  name: string;
  value: any;
  comment: string;
  constructor(data: IOption) {}
}

type TOptionArray = (IOption | ConfigOption)[];

class Config {
  constructor(options?: TOptionArray) {}
  push(data: ConfigOption | IOption | TOptionArray) {}
}

// Test example:

const config = new Config;

config.push({
  name: 'name',
  value: 0
});


new Config([{
  name: 'role',
  value: 'camelcase(this.element.name)'
}]);

I also attach an archive where the classes are in different files.
https://www.dropbox.com/s/u0ergcw4pt88oeu/typescri...
If you don't want to go into the archive, then here's how they are sorted into files:
// config/Config.ts

import IOption from './option/ConfigOption';
import ConfigOption from './option/ConfigOption';

export type TOptionArray = (IOption | ConfigOption)[];

export default class Config {
  constructor(options?: TOptionArray) {}
  push(data: ConfigOption | IOption | TOptionArray) {}
}

// config/option/ConfigOption.ts

import Config from '../Config';

export interface IOption {
  name: string;
  value?: any;
  comment?: string;
}

export default class ConfigOption {
  name: string;
  value: any;
  comment: string;
  constructor(data: IOption) {}
}


// index2.ts

import Config from './config/Config';

const config = new Config;

config.push({
  name: 'name',
  value: 0
});


new Config([{
  name: 'role',
  value: 'camelcase(this.element.name)'
}]);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Svyatoslav Khusamov, 2017-07-14
@khusamov

Instead , you need to write:
import { IOption } from './option/ConfigOption';

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question