Answer the question
In order to leave comments, you need to log in
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.
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)'
}]);
// 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
Instead
, you need to write:import { IOption } from './option/ConfigOption';
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question