Skip to content

WidgetManagerBase: Improve create_view return type #2662

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/base-manager/src/manager-base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ export abstract class ManagerBase<T> implements IWidgetManager {
* Make sure the view creation is not out of order with
* any state updates.
*/
create_view(model: DOMWidgetModel, options: any): Promise<DOMWidgetView>;
create_view(model: WidgetModel, options = {}): Promise<WidgetView> {
create_view<VT extends DOMWidgetView = DOMWidgetView>(model: DOMWidgetModel, options?: any): Promise<VT>;
create_view<VT extends WidgetView = WidgetView>(model: WidgetModel, options?: any): Promise<VT>;
create_view<VT extends WidgetView = WidgetView>(model: WidgetModel, options = {}): Promise<VT> {
const viewPromise = model.state_change = model.state_change.then(() => {
return this.loadClass(model.get('_view_name'),
model.get('_view_module'),
Expand Down
4 changes: 2 additions & 2 deletions packages/base/src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,8 @@ interface IWidgetManager {
* Make sure the view creation is not out of order with
* any state updates.
*/
create_view(model: DOMWidgetModel, options?: unknown): Promise<DOMWidgetView>;
create_view(model: WidgetModel, options?: unknown): Promise<WidgetView>;
create_view<VT extends DOMWidgetView = DOMWidgetView>(model: DOMWidgetModel, options?: unknown): Promise<VT>;
create_view<VT extends WidgetView = WidgetView>(model: WidgetModel, options?: unknown): Promise<VT>;

/**
* callback handlers specific to a view
Expand Down
6 changes: 4 additions & 2 deletions packages/base/src/widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,9 +691,11 @@ class WidgetView extends NativeView<WidgetModel> {
/**
* Create and promise that resolves to a child view of a given model
*/
create_child_view(child_model: WidgetModel, options = {}): Promise<DOMWidgetView> {
create_child_view<VT extends DOMWidgetView = DOMWidgetView>(child_model: DOMWidgetModel, options?: any): Promise<VT>;
create_child_view<VT extends WidgetView = WidgetView>(child_model: WidgetModel, options?: any): Promise<VT>;
create_child_view<VT extends WidgetView = WidgetView>(child_model: WidgetModel, options = {}): Promise<VT> {
options = { parent: this, ...options};
return this.model.widget_manager.create_view(child_model, options)
return this.model.widget_manager.create_view<VT>(child_model, options)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think once you have the overrides, you won't need to specify the type parameter here, as it should select the right override.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! Thanks you

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually it does not seem to compile without it:

src/widget.ts:696:9 - error TS2322: Type 'Promise<DOMWidgetView>' is not assignable to type 'Promise<VT>'.
  Type 'DOMWidgetView' is not assignable to type 'VT'.
    'DOMWidgetView' is assignable to the constraint of type 'VT', but 'VT' could be instantiated with a different subtype of constraint 'WidgetView'.

696         return this.model.widget_manager.create_view(child_model, options)
            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
697             .catch(utils.reject('Could not create child view', true));

I feel like this compilation error is not legit though, as DOMWidgetView is a subclass of WidgetView, what do you think?

.catch(utils.reject('Could not create child view', true));
}

Expand Down