-
foo.ts type Bar = {
}
export class Foo {
bar: Bar
} index.ts import { Foo } from './foo'
export class Index {
person: Foo
} output export declare type Bar = {};
declare class Foo {
bar: Bar;
}
export declare class Index {
person: Foo;
}
export {}; There are two issues:
why? I am very confused. |
Beta Was this translation helpful? Give feedback.
Replies: 11 comments
-
Hi,
In opposite to classes/functions/variables types/interfaces doesn't affect JS code and might be exported without any problem or affecting something. |
Beta Was this translation helpful? Give feedback.
-
I want to control the accessibility, in my example, I hope What can I do for that? |
Beta Was this translation helpful? Give feedback.
-
In this case, as I said, you need to export it from your entry-point explicitly. Just add
You export class |
Beta Was this translation helpful? Give feedback.
-
I agree with you, but how about a default export in import { Foo } from './foo'
export default class Index {
foo: Foo
}
export {
Foo
} output exports.Foo = Foo;
exports.default = Index;
Object.defineProperty(exports, '__esModule', { value: true }); But I only need |
Beta Was this translation helpful? Give feedback.
-
Can you please show what do you want to achieve (provide both JS and TS declaration)? |
Beta Was this translation helpful? Give feedback.
-
foo.ts // Bar is a internal type and hope not be exported
type Bar = { }
// Foo is a helper class and maybe used by outside
export class Foo {
bar: Bar
} main.ts import { Foo } from './foo'
// This is the main class for the library
export default class Main {
Foo: Foo
} run tsc, the foo.d.ts declare type Bar = {};
export declare class Foo {
bar: Bar;
}
export {}; main.d.ts import { Foo } from './foo';
export default class Main {
Foo: Foo;
} run export declare type Bar = {};
declare class Foo {
bar: Bar;
}
export default class Main {
Foo: Foo;
}
export {}; I want to use the import Main from 'main'
new Main()
new Main.Foo()
// how to import Foo type?
function doSomething(a: Foo) {
} I just write so, how can I achieve? |
Beta Was this translation helpful? Give feedback.
-
According your case: if want to export type, I believe you need to export interface or something to be able to use it. If your d.ts will export import { Foo } from 'main';
new Foo(); // ReferenceError here, because there is no Foo. |
Beta Was this translation helpful? Give feedback.
-
I think
why? |
Beta Was this translation helpful? Give feedback.
-
I find exporting I really don't know how to export the type I want. |
Beta Was this translation helpful? Give feedback.
-
Because it's foo.ts, not index.ts?
You need to understand, that name You want to export type of class Foo (which cannot be called and used in the code).
You can try to export that type with name Foo from your index.ts file (but I believe you don't need to have importing Foo to avoid names confusing). |
Beta Was this translation helpful? Give feedback.
-
@timocov Thank you. |
Beta Was this translation helpful? Give feedback.
@timocov Thank you.