Reputation: 36311
I have the following TypeScript
interface and method:
namespace a {
export interface componentType<T extends component> {
new(element?: HTMLElement): T
runStaticTick(comp: componentType<T>, tick?: number): void
}
export abstract class component {
public static components: component[] = []
public findComponent<T extends component>(comp: componentType<T>, callback?: (comp: T) => void) {
let c = component.components.find(c => c instanceof comp) as T
c instanceof component && typeof callback == 'function' && callback(c)
return c
}
}
export abstract class element extends component { }
}
This is the .d.ts
declare namespace a {
interface componentType<T extends component> {
new(element?: HTMLElement): T;
}
abstract class component {
findComponent<T extends component>(comp: componentType<T>, callback?: (comp: T) => void): T;
}
abstract class element extends component { }
}
Then, to test if the method works, I have this JavaScript
:
/// <reference path="../src/test.d.ts"/>
class checklist extends a.element {
sayHi() { console.log('Hi') }
}
class myclass extends a.component {
created() {
this.findComponent(checklist, item => {
item.sayHi()
})
}
}
The code works in the browser, but in the editor, it says that item
is of type component
when it should be of type checklist
. Is there a way to fix this or is this an issue with typescript?
For the full project: https://github.com/TheColorRed/horsepower
I figured out what is causing the issue:
export interface componentType<T extends component> {
new(element?: HTMLElement): T
runStaticTick(comp: componentType<T>, tick?: number): void
}
If I comment out runStaticTick
it fixes the intellisense problem. But then I have errors saying that it doesn't exist:
error TS2339: Property 'runStaticTick' does not exist on type 'componentType'
Upvotes: 0
Views: 1318
Reputation: 36311
The issue was because of the property within the method runStaticTick
.
Instead using using
comp: componentType<T>
I should be using
comp: componentType<component>
For some reason, T
was causing issues of which I do not know. If someone knows what that issue is, please let me know.
Upvotes: 1