dts-dom
dts-dom copied to clipboard
references between different namespaces
The following
const intfA = dom.create.interface('A');
const intfB = dom.create.interface('B');
intfB.baseTypes.push(intfA);
const foo = dom.create.namespace('Foo');
const bar = dom.create.namespace('Bar');
foo.members.push(intfB);
foo.members.push(bar);
bar.members.push(intfA);
console.log(dom.emit(foo));
generates:
declare namespace Foo {
interface B extends A{
}
namespace Bar {
interface A {
}
}
}
Which causes error because A isn't defined in namespace Foo. It should be interface B extends Bar.A.
Is this intended and the solution is to supply a dummy ObjectTypeReference with full relative name, or it's a bug?
The library doesn't automatically determine a correctly-qualified name when there's a reference to another type. It's more of a feature request than a bug, and a rather complicated one at that (e.g. it creates possible error situations, because in some cases a type in the same file doesn't have any name by which you can legally reference it).
You should (for now) use a NamedTypeReference, though it's technically incorrect because the name would be an illegal identifier. That said, it would work.
The right thing would be to add the type QualifiedTypeReference that accepted a namespace portion and a TypeReference and emitted the dotted name.
Considering that the reference is part of the DOM, dts-dom could possible figure out the qualified name on its own. I'm not entirely sure what pitfalls there might be, but maybe dts-dom could crawl from top to find the path to one type and the other, then based on the paths, either use relative qualified name or full one?
For now I will try to implement it locally for my use case. Although it would have to be done via dummy ObjectTypeReference as baseType/implements expects (can't pass in NamedTypeReference).