angular / angular/components

bug(drag-drop): Wrong type assumed for dropped item in CdkDropList's cdkDropListEntered (alias for "entered")

オープン
#29,212 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る
area: cdk/drag-drop P4
主要言語
TypeScript
スター
25k
フォーク
6.8k
平均マージ
1日 8時間
マージ済み PR(30日)
91

説明

### Is this a regression?

- [ ] Yes, this behavior used to work in the previous version

### The previous version in which this bug was not present was

_No response_

### Description

I'm facing an issue where Typescript won't allow me to use strongly typed version of `CdkDragEnter`. In a service I have a method with defined types for `CdkDragEnter`'s item and container data.

https://github.com/angular/components/blob/main/src/cdk/drag-drop/directives/drop-list.ts#L138

`CdkDropList`'s `entered` is typed like this: `EventEmitter>` which results in `CdkDragEnter`'s 2nd type argument assuming the same type as the 1st argument. That way it won't allow me to define two different types for the container's and the item's data.

### Reproduction

StackBlitz link:
Steps to reproduce:
1. define an element in template
2. add `cdkDropList` directive
3. add arbitrary data to `cdkDropListData` directive
4. add `(cdkDropListEntered)="onDropListEnter($event)` to react to the event
5. define `onDropListEnter` liks this:
```ts
onDropListEnter(event: CdkDragEnter) {
this.dropListEnterEvent.emit(event);
}
```

### Expected Behavior

Typing for `entered` Event Emitter allows different types between item and container data.
This is already the case for the `dropped` Event Emitter which is typed like this: `EventEmitter>`
So I'm guessing that this could already be a possible solution?

### Actual Behavior

`$event` in `(cdkDropListEntered)="onDropListEnter($event)` gets highlighted in red and build fails because `The types of 'container.data' are incompatible between these types.`

### Environment

Angular CLI: 17.3.3
Node: 20.11.0
Package Manager: npm 10.2.4
OS: darwin arm64

Angular: 17.3.4
... animations, common, compiler, compiler-cli, core, forms
... localize, platform-browser, platform-browser-dynamic, router

Package Version
---------------------------------------------------------
@angular-devkit/architect 0.1703.3
@angular-devkit/build-angular 17.3.3
@angular-devkit/core 17.3.3
@angular-devkit/schematics 17.3.3
@angular/cdk 17.3.3
@angular/cli 17.3.3
@schematics/angular 17.3.3
rxjs 7.8.1
typescript 5.3.3
zone.js 0.14.4

コントリビューションガイド

コントリビューションガイドを開く

調査の方向性

src/cdk/drag-drop/directives/drop-list.ts の 138 行目付近から始め、entered EventEmitter の型を、CdkDragEnter のジェネリックパラメーターおよび dropped emitter と併せて確認します。完了条件は、cdkDropListEntered handler がコンテナーのデータとドラッグアイテムのデータに異なる型を使用でき、issue の例がコンパイルできることです。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
angular, typescript
領域
frontend
issue の種類
バグ
難易度
2/5
見積もり時間
1〜3時間
活発さ
停滞
明瞭さ
明確に書かれている
初心者へのやさしさ
48/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。