如果按文件分隔打字稿 类,则会发生错误。怎么会这样?
If you separate the typescript classes by files, then an error occurs. How can this be?
哪些地方会出错?
当所有 类 都在同一个文件中时,不会出现该错误。
但是一旦我将 类 放在不同的文件中,就会出现以下错误:
/index2.ts(8,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(14,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:36:36 - 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)'
}]);
https://www.dropbox.com/s/u0ergcw4pt88oeu/typescriptproblem.zip?dl=0
文件代码布局:
// 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)'
}]);
您可以省略 export 和 import 关键字,这些不是必需的,即使您使用单独的 .ts 文件也是如此。
除非您想使用模块或命名空间,但您似乎不想那样做?
您没有正确导入。
您应该更改 config/option/ConfigOption.ts
以删除默认导出:
export class ConfigOption { // Removed the `default` keyword.
并且在 config/Config.ts
中,像这样导入:
import { IOption, ConfigOption } from './option/ConfigOption';
在我自己的代码中,我完全取消了默认导出以避免这样的错误。您可以保留默认导出,但我不建议这样做。
你的方式是这样的:
import IOption from './option/ConfigOption';
import ConfigOption from './option/ConfigOption';
这将从 ./option/ConfigOption.ts
导入默认导出并将其本地分配给 IOption
和 ConfigOption
。因此,您的 IOption
与 ./option/ConfigOption.ts
中的 IOption
不对应,而是对应于该模块的默认导出 ConfigOption
.
哪些地方会出错? 当所有 类 都在同一个文件中时,不会出现该错误。 但是一旦我将 类 放在不同的文件中,就会出现以下错误:
/index2.ts(8,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(14,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:36:36 - 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)'
}]);
https://www.dropbox.com/s/u0ergcw4pt88oeu/typescriptproblem.zip?dl=0
文件代码布局:
// 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)'
}]);
您可以省略 export 和 import 关键字,这些不是必需的,即使您使用单独的 .ts 文件也是如此。
除非您想使用模块或命名空间,但您似乎不想那样做?
您没有正确导入。
您应该更改 config/option/ConfigOption.ts
以删除默认导出:
export class ConfigOption { // Removed the `default` keyword.
并且在 config/Config.ts
中,像这样导入:
import { IOption, ConfigOption } from './option/ConfigOption';
在我自己的代码中,我完全取消了默认导出以避免这样的错误。您可以保留默认导出,但我不建议这样做。
你的方式是这样的:
import IOption from './option/ConfigOption';
import ConfigOption from './option/ConfigOption';
这将从 ./option/ConfigOption.ts
导入默认导出并将其本地分配给 IOption
和 ConfigOption
。因此,您的 IOption
与 ./option/ConfigOption.ts
中的 IOption
不对应,而是对应于该模块的默认导出 ConfigOption
.