From a2cd62493ce2740a4844b0aa48006bc805f58d53 Mon Sep 17 00:00:00 2001 From: Bas Bosman Date: Thu, 7 Feb 2019 22:44:35 +0100 Subject: [PATCH] refactor: remove Constructable "hack" and fix exposed typing errors --- packages/jit-html/test/integration/util.ts | 2 +- packages/jit/src/binding-command.ts | 7 ++-- packages/kernel/src/di.ts | 33 ++++++++++--------- packages/kernel/src/interfaces.ts | 11 +++---- packages/kernel/test/di.spec.ts | 22 ++++++------- packages/router/src/nav-route.ts | 2 +- packages/router/test/e2e/layouts/src/app.ts | 2 +- packages/runtime-html/src/dom.ts | 2 +- packages/runtime-html/src/projectors.ts | 2 -- packages/runtime/src/binding/connectable.ts | 1 - .../runtime/src/resources/binding-behavior.ts | 7 ++-- .../runtime/src/resources/custom-attribute.ts | 9 ++--- .../runtime/src/resources/custom-element.ts | 7 ++-- .../runtime/src/resources/value-converter.ts | 2 +- packages/runtime/src/templating/bindable.ts | 2 +- .../runtime/src/templating/lifecycle-bind.ts | 1 - packages/runtime/test/binding/ast.spec.ts | 4 --- packages/runtime/test/binding/binding.spec.ts | 1 - packages/runtime/test/renderer.spec.ts | 2 +- .../resources/custom-element.resource.spec.ts | 2 +- 20 files changed, 59 insertions(+), 62 deletions(-) diff --git a/packages/jit-html/test/integration/util.ts b/packages/jit-html/test/integration/util.ts index 6ea51568829..c884e691a40 100644 --- a/packages/jit-html/test/integration/util.ts +++ b/packages/jit-html/test/integration/util.ts @@ -47,7 +47,7 @@ export function setup(ctx: HTMLTestContext, template: string | Node, $class: Con const host = ctx.createElement('app'); const au = new Aurelia(container); const App = CustomElementResource.define({ name: 'app', template }, $class); - const component = new App(); + const component = new App() as any; return { container, lifecycle, host, au, component, observerLocator }; } diff --git a/packages/jit/src/binding-command.ts b/packages/jit/src/binding-command.ts index 95c2162ed74..39d4dffd39d 100644 --- a/packages/jit/src/binding-command.ts +++ b/packages/jit/src/binding-command.ts @@ -62,11 +62,12 @@ function isType(this: IBindingCommandResource, Type: T & Partial(this: IBindingCommandResource, name: string, ctor: T): T & IBindingCommandType; function define(this: IBindingCommandResource, definition: IBindingCommandDefinition, ctor: T): T & IBindingCommandType; function define(this: IBindingCommandResource, nameOrDefinition: string | IBindingCommandDefinition, ctor: T): T & IBindingCommandType { - const Type = ctor as T & Writable; + const Type = ctor as T & IBindingCommandType; + const WritableType = Type as T & Writable; const description = typeof nameOrDefinition === 'string' ? { name: nameOrDefinition, target: null } : nameOrDefinition; - Type.kind = BindingCommandResource; - Type.description = description; + WritableType.kind = BindingCommandResource as IBindingCommandResource; + WritableType.description = description; Type.register = register; return Type; diff --git a/packages/kernel/src/di.ts b/packages/kernel/src/di.ts index 6c125c0426b..3d2c32c90e0 100644 --- a/packages/kernel/src/di.ts +++ b/packages/kernel/src/di.ts @@ -102,6 +102,9 @@ if (!('getOwnMetadata' in Reflect)) { return function(target: Function): void { (target as IIndexable)[metadataKey] = metadataValue; }; + } as (metadataKey: any, metadataValue: any) => { + (target: Function): void; + (target: Object, propertyKey: string | symbol): void; }; } // tslint:enable:no-any ban-types @@ -575,7 +578,7 @@ export class Container implements IContainer { if (isRegistry(current)) { current.register(this); } else if (isClass(current)) { - Registration.singleton(current, current).register(this); + Registration.singleton(current, current as Constructable).register(this); } else { keys = Object.keys(current); j = 0; @@ -841,49 +844,49 @@ function buildAllResponse(resolver: IResolver, handler: IContainer, requestor: I /** @internal */ export const classInvokers: IInvoker[] = [ { - invoke(container: IContainer, Type: T): K { - return new Type(); + invoke>(container: IContainer, Type: T): InstanceType { + return new Type() as InstanceType; }, invokeWithDynamicDependencies }, { - invoke(container: IContainer, Type: T, deps: Key[]): K { - return new Type(container.get(deps[0])); + invoke>(container: IContainer, Type: T, deps: Key[]): InstanceType { + return new Type(container.get(deps[0])) as InstanceType; }, invokeWithDynamicDependencies }, { - invoke(container: IContainer, Type: T, deps: Key[]): K { - return new Type(container.get(deps[0]), container.get(deps[1])); + invoke>(container: IContainer, Type: T, deps: Key[]): InstanceType { + return new Type(container.get(deps[0]), container.get(deps[1])) as InstanceType; }, invokeWithDynamicDependencies }, { - invoke(container: IContainer, Type: T, deps: Key[]): K { - return new Type(container.get(deps[0]), container.get(deps[1]), container.get(deps[2])); + invoke>(container: IContainer, Type: T, deps: Key[]): InstanceType { + return new Type(container.get(deps[0]), container.get(deps[1]), container.get(deps[2])) as InstanceType; }, invokeWithDynamicDependencies }, { - invoke(container: IContainer, Type: T, deps: Key[]): K { + invoke>(container: IContainer, Type: T, deps: Key[]): InstanceType { return new Type( container.get(deps[0]), container.get(deps[1]), container.get(deps[2]), container.get(deps[3]) - ); + ) as InstanceType; }, invokeWithDynamicDependencies }, { - invoke(container: IContainer, Type: T, deps: Key[]): K { + invoke>(container: IContainer, Type: T, deps: Key[]): InstanceType { return new Type( container.get(deps[0]), container.get(deps[1]), container.get(deps[2]), container.get(deps[3]), container.get(deps[4]) - ); + ) as InstanceType; }, invokeWithDynamicDependencies } @@ -896,12 +899,12 @@ export const fallbackInvoker: IInvoker = { }; /** @internal */ -export function invokeWithDynamicDependencies( +export function invokeWithDynamicDependencies>( container: IContainer, Type: T, staticDependencies: Key[], dynamicDependencies: Key[] -): K { +): InstanceType { let i = staticDependencies.length; let args: Key[] = new Array(i); let lookup: Key; diff --git a/packages/kernel/src/interfaces.ts b/packages/kernel/src/interfaces.ts index 3857c8cba1d..28c4d28e795 100644 --- a/packages/kernel/src/interfaces.ts +++ b/packages/kernel/src/interfaces.ts @@ -66,8 +66,8 @@ export interface IDisposable { } export type Constructable = { - // tslint:disable-next-line:no-any no-useless-intersection callable-types - new(...args: unknown[]): T & any; // this is a "hack" to stop typescript from nagging about the type parameter T being unused (the parameter may be used for type inference) + // tslint:disable-next-line:callable-types + new(...args: unknown[]): T; }; export type Class = C & { @@ -84,11 +84,11 @@ export type ConstructableClass = C & { new(...args: unknown[]): T & { constructor: C }; }; -export type InterfaceSymbol = (target: Injectable, property: string, index: number) => any; +export type InterfaceSymbol = (target: Injectable, property: string, index: number) => unknown; export type InjectArray = ReadonlyArray; -export type Injectable = Constructable & { inject?: (InterfaceSymbol|Constructable)[] }; +export type Injectable = Constructable & { inject?: (InterfaceSymbol | Constructable)[] }; export type IIndexable = T & { [key: string]: unknown }; @@ -120,8 +120,7 @@ export type Writable = { export type Diff = ({[P in T]: P } & {[P in U]: never } & { [x: string]: never })[T]; -// tslint:disable-next-line:no-any -export type Omit = T extends any ? Pick> : never; +export type Omit = T extends {} ? Pick> : never; export type Overwrite = Pick> & T2; diff --git a/packages/kernel/test/di.spec.ts b/packages/kernel/test/di.spec.ts index 4318b8dbd6d..ae7367b501a 100644 --- a/packages/kernel/test/di.spec.ts +++ b/packages/kernel/test/di.spec.ts @@ -1130,25 +1130,25 @@ describe(`The classInvokers object`, function () { class Dep6 {} it(`invoke() handles 0 deps`, function () { - const actual: InstanceType> = classInvokers[0].invoke(container, Foo, []); + const actual = classInvokers[0].invoke(container, Foo, []) as InstanceType>; expect(actual.args.length).to.equal(0); }); it(`invoke() handles 1 dep`, function () { - const actual: InstanceType> = classInvokers[1].invoke(container, Foo, [Dep1]); + const actual = classInvokers[1].invoke(container, Foo, [Dep1]) as InstanceType>; expect(actual.args.length).to.equal(1); expect(actual.args[0]).to.be.instanceof(Dep1); }); it(`invoke() handles 2 deps`, function () { - const actual: InstanceType> = classInvokers[2].invoke(container, Foo, [Dep1, Dep2]); + const actual = classInvokers[2].invoke(container, Foo, [Dep1, Dep2]) as InstanceType>; expect(actual.args.length).to.equal(2); expect(actual.args[0]).to.be.instanceof(Dep1); expect(actual.args[1]).to.be.instanceof(Dep2); }); it(`invoke() handles 3 deps`, function () { - const actual: InstanceType> = classInvokers[3].invoke(container, Foo, [Dep1, Dep2, Dep3]); + const actual = classInvokers[3].invoke(container, Foo, [Dep1, Dep2, Dep3]) as InstanceType>; expect(actual.args.length).to.equal(3); expect(actual.args[0]).to.be.instanceof(Dep1); expect(actual.args[1]).to.be.instanceof(Dep2); @@ -1156,7 +1156,7 @@ describe(`The classInvokers object`, function () { }); it(`invoke() handles 4 deps`, function () { - const actual: InstanceType> = classInvokers[4].invoke(container, Foo, [Dep1, Dep2, Dep3, Dep4]); + const actual = classInvokers[4].invoke(container, Foo, [Dep1, Dep2, Dep3, Dep4]) as InstanceType>; expect(actual.args.length).to.equal(4); expect(actual.args[0]).to.be.instanceof(Dep1); expect(actual.args[1]).to.be.instanceof(Dep2); @@ -1165,7 +1165,7 @@ describe(`The classInvokers object`, function () { }); it(`invoke() handles 5 deps`, function () { - const actual: InstanceType> = classInvokers[5].invoke(container, Foo, [Dep1, Dep2, Dep3, Dep4, Dep5]); + const actual = classInvokers[5].invoke(container, Foo, [Dep1, Dep2, Dep3, Dep4, Dep5]) as InstanceType>; expect(actual.args.length).to.equal(5); expect(actual.args[0]).to.be.instanceof(Dep1); expect(actual.args[1]).to.be.instanceof(Dep2); @@ -1205,28 +1205,28 @@ describe(`The invokeWithDynamicDependencies function`, function () { }); it(_`handles staticDeps is ${deps}`, function () { - const actual = invokeWithDynamicDependencies(container, Foo, deps, []) as Foo; + const actual = invokeWithDynamicDependencies(container, Foo, deps, []); expect(actual.args).to.deep.equal(deps.map(d => `static${d}`)); }); it(`handles dynamicDeps is null`, function () { - const actual = invokeWithDynamicDependencies(container, Foo, [], null) as Foo; + const actual = invokeWithDynamicDependencies(container, Foo, [], null); expect(actual.args.length).to.equal(1); expect(actual.args[0]).to.equal(null); }); it(`handles dynamicDeps is undefined`, function () { - const actual = invokeWithDynamicDependencies(container, Foo, [], undefined) as Foo; + const actual = invokeWithDynamicDependencies(container, Foo, [], undefined); expect(actual.args.length).to.equal(0); }); it(_`handles dynamicDeps is ${deps}`, function () { - const actual = invokeWithDynamicDependencies(container, Foo, [], deps) as Foo; + const actual = invokeWithDynamicDependencies(container, Foo, [], deps); expect(actual.args).to.deep.equal(deps); }); it(_`handles staticDeps is ${deps} and dynamicDeps is ${deps}`, function () { - const actual = invokeWithDynamicDependencies(container, Foo, deps, deps) as Foo; + const actual = invokeWithDynamicDependencies(container, Foo, deps, deps); expect(actual.args[0]).to.equal(`static${deps[0]}`); expect(actual.args[1]).to.equal(`static${deps[1]}`); expect(actual.args[2]).to.equal(`static${deps[2]}`); diff --git a/packages/router/src/nav-route.ts b/packages/router/src/nav-route.ts index 7853258d90c..84301a5a243 100644 --- a/packages/router/src/nav-route.ts +++ b/packages/router/src/nav-route.ts @@ -78,7 +78,7 @@ export class NavRoute { const viewportComponent = route as IViewportComponent; instructions.push(new ViewportInstruction(viewportComponent.component, viewportComponent.viewport, viewportComponent.parameters)); } else { - instructions.push(new ViewportInstruction(route as Partial>)); + instructions.push(new ViewportInstruction(route as Partial)); } } return instructions; diff --git a/packages/router/test/e2e/layouts/src/app.ts b/packages/router/test/e2e/layouts/src/app.ts index f0c0c41bdf8..06e953a622d 100644 --- a/packages/router/test/e2e/layouts/src/app.ts +++ b/packages/router/test/e2e/layouts/src/app.ts @@ -17,6 +17,6 @@ import { customElement } from '../../../../../runtime'; ` }) export class App { constructor(private readonly router: Router) { - this.router.activate(); + this.router.activate().catch((error: Error) => { throw error; }); } } diff --git a/packages/runtime-html/src/dom.ts b/packages/runtime-html/src/dom.ts index 362c8815f5a..5acf3dbfdd6 100644 --- a/packages/runtime-html/src/dom.ts +++ b/packages/runtime-html/src/dom.ts @@ -405,7 +405,7 @@ export class NodeSequenceFactory implements NodeSequenceFactory { private readonly dom: IDOM; private readonly deepClone: boolean; private readonly node: Node; - private readonly Type: Constructable; + private readonly Type: Constructable; constructor(dom: IDOM, markupOrNode: string | Node) { this.dom = dom; diff --git a/packages/runtime-html/src/projectors.ts b/packages/runtime-html/src/projectors.ts index 78bc97008c0..fdce799d836 100644 --- a/packages/runtime-html/src/projectors.ts +++ b/packages/runtime-html/src/projectors.ts @@ -62,7 +62,6 @@ export class ShadowDOMProjector implements IElementProjector { } this.shadowRoot = host.attachShadow(shadowOptions); this.host.$customElement = $customElement; - // tslint:disable-next-line:no-unnecessary-type-assertion // this is a false positive this.shadowRoot.$customElement = $customElement as ICustomElement; } @@ -105,7 +104,6 @@ export class ContainerlessProjector implements IElementProjector { this.childNodes = PLATFORM.emptyArray; } - // tslint:disable-next-line:no-unnecessary-type-assertion // this is a false positive this.host = dom.convertToRenderLocation(host) as CustomElementHost; this.host.$customElement = $customElement; } diff --git a/packages/runtime/src/binding/connectable.ts b/packages/runtime/src/binding/connectable.ts index bb69505f60a..b139c511123 100644 --- a/packages/runtime/src/binding/connectable.ts +++ b/packages/runtime/src/binding/connectable.ts @@ -121,7 +121,6 @@ function connectableDecorator(target: DecoratableConnectable; } diff --git a/packages/runtime/src/resources/binding-behavior.ts b/packages/runtime/src/resources/binding-behavior.ts index 6ecc6a5d685..977e5ec6823 100644 --- a/packages/runtime/src/resources/binding-behavior.ts +++ b/packages/runtime/src/resources/binding-behavior.ts @@ -49,13 +49,14 @@ function define(this: IBindingBehaviorR function define(this: IBindingBehaviorResource, name: string, ctor: T): T & IBindingBehaviorType; function define(this: IBindingBehaviorResource, nameOrDefinition: string | IBindingBehaviorDefinition, ctor: T): T & IBindingBehaviorType; function define(this: IBindingBehaviorResource, nameOrDefinition: string | IBindingBehaviorDefinition, ctor: T): T & IBindingBehaviorType { - const Type = ctor as T & Writable; + const Type = ctor as T & IBindingBehaviorType; + const WritableType = Type as T & Writable>; const description = typeof nameOrDefinition === 'string' ? { name: nameOrDefinition } : nameOrDefinition; - Type.kind = BindingBehaviorResource; - Type.description = description; + WritableType.kind = BindingBehaviorResource as IBindingBehaviorResource; + WritableType.description = description; Type.register = register; return Type; diff --git a/packages/runtime/src/resources/custom-attribute.ts b/packages/runtime/src/resources/custom-attribute.ts index 2f19433da52..1d5f1e2d381 100644 --- a/packages/runtime/src/resources/custom-attribute.ts +++ b/packages/runtime/src/resources/custom-attribute.ts @@ -126,12 +126,13 @@ function define(this: ICustomAttributeResource, name: string, ctor: T): T & ICustomAttributeType; function define(this: ICustomAttributeResource, nameOrDefinition: string | IAttributeDefinition, ctor: T): T & ICustomAttributeType; function define(this: ICustomAttributeResource, nameOrDefinition: string | IAttributeDefinition, ctor: T): T & ICustomAttributeType { - const Type = ctor as T & Writable; + const Type = ctor as T & ICustomAttributeType; + const WritableType = Type as T & Writable>; const description = createCustomAttributeDescription(typeof nameOrDefinition === 'string' ? { name: nameOrDefinition } : nameOrDefinition, Type); const proto: Writable = Type.prototype; - Type.kind = CustomAttributeResource; - Type.description = description; + WritableType.kind = CustomAttributeResource as ICustomAttributeResource; + WritableType.description = description; Type.register = registerAttribute; proto.$hydrate = $hydrateAttribute; @@ -181,7 +182,7 @@ function define; + const Type = (ctor === null ? class HTMLOnlyElement { /* HTML Only */ } : ctor) as T & ICustomElementType; + const WritableType = Type as Writable>; const description = buildTemplateDefinition(Type, nameOrDefinition); const proto: Writable = Type.prototype; - Type.kind = CustomElementResource; + WritableType.kind = CustomElementResource as ICustomElementResource; Type.description = description; Type.register = registerElement; @@ -191,7 +192,7 @@ function define(this: IValueConverterRe Type.description = description; Type.register = register; - return Type; + return Type as T & IValueConverterType; } export const ValueConverterResource = { diff --git a/packages/runtime/src/templating/bindable.ts b/packages/runtime/src/templating/bindable.ts index 7a21f9bc985..3ad3ad49ce3 100644 --- a/packages/runtime/src/templating/bindable.ts +++ b/packages/runtime/src/templating/bindable.ts @@ -30,7 +30,7 @@ export function bindable).add(config); }; if (arguments.length > 1) { // Non invocation: diff --git a/packages/runtime/src/templating/lifecycle-bind.ts b/packages/runtime/src/templating/lifecycle-bind.ts index e2b65caf006..b651730859b 100644 --- a/packages/runtime/src/templating/lifecycle-bind.ts +++ b/packages/runtime/src/templating/lifecycle-bind.ts @@ -233,7 +233,6 @@ export function $unbindElement(this: Writable, flags: LifecycleFlags) binding = binding.$prevBinding; } - // tslint:disable-next-line:no-unnecessary-type-assertion // this is a false positive (this.$scope as Writable).parentScope = null; // remove isBound and isUnbinding flags diff --git a/packages/runtime/test/binding/ast.spec.ts b/packages/runtime/test/binding/ast.spec.ts index e18fb47bb1d..b3a6e3e5d0a 100644 --- a/packages/runtime/test/binding/ast.spec.ts +++ b/packages/runtime/test/binding/ast.spec.ts @@ -268,10 +268,6 @@ describe('AST', function () { const SimpleLogicalANDList: [string, Binary][] = [ [`$36&&$37`, new Binary('&&', new AccessScope('$36'), new AccessScope('$37'))] ]; - const SimpleIsLogicalANDList: [string, IsBinary][] = [ - ...SimpleIsEqualityList, - ...SimpleLogicalANDList - ]; // This forms the group Precedence.LogicalOR const SimpleLogicalORList: [string, Binary][] = [ diff --git a/packages/runtime/test/binding/binding.spec.ts b/packages/runtime/test/binding/binding.spec.ts index e285e05b3fa..e49c58ce83a 100644 --- a/packages/runtime/test/binding/binding.spec.ts +++ b/packages/runtime/test/binding/binding.spec.ts @@ -117,7 +117,6 @@ describe('Binding', function () { } const container = RuntimeBasicConfiguration.createContainer(); const observerLocator = createObserverLocator(container); - const lifecycle = container.get(ILifecycle) as Lifecycle; const target = {val: 0}; const sut = new Binding(expr as any, target, 'val', BindingMode.toView, observerLocator, container); const scope = Scope.create(LF.none, ctx, null); diff --git a/packages/runtime/test/renderer.spec.ts b/packages/runtime/test/renderer.spec.ts index e698de99c7a..fc92848a2ba 100644 --- a/packages/runtime/test/renderer.spec.ts +++ b/packages/runtime/test/renderer.spec.ts @@ -177,7 +177,7 @@ describe('Renderer', function () { for (const instructions of [[], [new SetPropertyInstruction('bar', 'foo')]] as any[]) { const instruction = new HydrateAttributeInstruction(res, instructions) as any; it(_`instruction=${instruction}`, function () { - const { sut, dom, renderable, target, renderContext, renderingEngine } = setup(); + const { sut, dom, renderable, target, renderContext } = setup(); sut.instructionRenderers[instruction.type].render(LF.none, dom, renderContext, renderable, target, instruction); diff --git a/packages/runtime/test/resources/custom-element.resource.spec.ts b/packages/runtime/test/resources/custom-element.resource.spec.ts index 3181a1f3b9b..60039537467 100644 --- a/packages/runtime/test/resources/custom-element.resource.spec.ts +++ b/packages/runtime/test/resources/custom-element.resource.spec.ts @@ -39,7 +39,7 @@ describe('CustomElementResource', function () { it(`returns false when given a resource with the wrong kind`, function () { const type = CustomAttributeResource.define('foo', class Foo {}); - expect(CustomElementResource.isType(type)).to.equal(false); + expect(CustomElementResource.isType(type as any)).to.equal(false); }); });