Skip to content

Commit a0f4f8f

Browse files
claude[bot]claude
andauthored
docs: narrow clipboardItem.getType() return type from its MIME type (#54342)
docs: narrow clipboardItem.getType() return type from its MIME type (#54300) * docs: narrow clipboardItem.getType() return type from its MIME type Type getType() with a generic MIME type parameter and a conditional return type so a literal `electron application/bookmark` resolves to a ClipboardBookmark, any other literal MIME type resolves to a Blob, and a non-literal string still resolves to the union. Adds ts-smoke cases for all three and drops the now-unneeded `instanceof Blob` check from the `text/uri-list` example. Claude-Session: https://claude.ai/code/session_01HmVsAQiHawruFHijTtkzV9 * build: bump @electron/docs-parser to 3.0.2 Claude-Session: https://claude.ai/code/session_01HmVsAQiHawruFHijTtkzV9 --------- (cherry picked from commit 70d678d) Claude-Session: https://claude.ai/code/session_01A6qgQ8Q6Q5rwKtmambSd9V Co-authored-by: Claude <[email protected]>
1 parent a794c1d commit a0f4f8f

4 files changed

Lines changed: 26 additions & 13 deletions

File tree

‎docs/api/clipboard-item.md‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,10 +112,8 @@ async function readFiles() {
112112
const [item] = await clipboard.read()
113113
if (item.types.includes('text/uri-list')) {
114114
const blob = await item.getType('text/uri-list')
115-
if (blob instanceof Blob) {
116-
const uriList = await blob.text()
117-
return uriList.split(/\r?\n/).filter(Boolean)
118-
}
115+
const uriList = await blob.text()
116+
return uriList.split(/\r?\n/).filter(Boolean)
119117
}
120118
return []
121119
}
@@ -133,11 +131,11 @@ the platform clipboard currently makes available.
133131

134132
### Instance Methods
135133

136-
#### `clipboardItem.getType(type)`
134+
#### `clipboardItem.getType<T extends string>(type)`
137135

138-
* `type` string - mime type to retrieve.
136+
* `type` T - mime type to retrieve.
139137

140-
Returns `Promise<Blob> | Promise<ClipboardBookmark>` - Resolves with the payload for the
138+
Returns `Promise<string extends T ? (Blob | ClipboardBookmark) : T extends 'electron application/bookmark' ? ClipboardBookmark : Blob>` - Resolves with the payload for the
141139
given MIME type. Modeled after the W3C
142140
[`ClipboardItem.getType`](https://developer.mozilla.org/en-US/docs/Web/API/ClipboardItem/getType)
143141
method. The promise resolves to a `Blob` for most MIME types; the one

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"@azure/storage-blob": "^12.28.0",
88
"@datadog/datadog-ci": "^5.17.0",
99
"@electron/asar": "^4.0.1",
10-
"@electron/docs-parser": "^3.0.1",
10+
"@electron/docs-parser": "^3.0.2",
1111
"@electron/fiddle-core": "^1.3.4",
1212
"@electron/github-app-auth": "^3.2.0",
1313
"@electron/lint-roller": "^4.0.0",

‎spec/ts-smoke/electron/main.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,6 +1086,21 @@ app.whenReady().then(() => {
10861086
console.log(type, 'url' in payload ? payload.url : payload.size);
10871087
}
10881088
}
1089+
1090+
// `getType` narrows its result from the MIME type argument: a bookmark for
1091+
// the bookmark MIME type, a Blob for any other literal MIME type, and the
1092+
// union when the MIME type is not known statically.
1093+
const [firstItem] = items;
1094+
const bookmark: Electron.ClipboardBookmark = await firstItem.getType('electron application/bookmark');
1095+
console.log(bookmark.title, bookmark.url);
1096+
const html: Blob = await firstItem.getType('text/html');
1097+
console.log(html.size);
1098+
const mimeType: string = firstItem.types[0];
1099+
const unknownPayload = await firstItem.getType(mimeType);
1100+
const eitherPayload: Blob | Electron.ClipboardBookmark = unknownPayload;
1101+
// @ts-expect-error A MIME type that is not a literal may resolve to a ClipboardBookmark
1102+
const notNarrowed: Blob = unknownPayload;
1103+
console.log(eitherPayload, notNarrowed);
10891104
})();
10901105

10911106
// crash-reporter

‎yarn.lock‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ __metadata:
371371
"@azure/storage-blob": "npm:^12.28.0"
372372
"@datadog/datadog-ci": "npm:^5.17.0"
373373
"@electron/asar": "npm:^4.0.1"
374-
"@electron/docs-parser": "npm:^3.0.1"
374+
"@electron/docs-parser": "npm:^3.0.2"
375375
"@electron/fiddle-core": "npm:^1.3.4"
376376
"@electron/github-app-auth": "npm:^3.2.0"
377377
"@electron/lint-roller": "npm:^4.0.0"
@@ -520,9 +520,9 @@ __metadata:
520520
languageName: node
521521
linkType: hard
522522

523-
"@electron/docs-parser@npm:^3.0.1":
524-
version: 3.0.1
525-
resolution: "@electron/docs-parser@npm:3.0.1"
523+
"@electron/docs-parser@npm:^3.0.2":
524+
version: 3.0.2
525+
resolution: "@electron/docs-parser@npm:3.0.2"
526526
dependencies:
527527
"@types/markdown-it": "npm:^14.1.2"
528528
chai: "npm:^5.1.1"
@@ -534,7 +534,7 @@ __metadata:
534534
bin:
535535
electron-docs-linter: dist/bin.js
536536
electron-docs-parser: dist/bin.js
537-
checksum: 10c0/2ea17a8da29e4f3bf3b0ff22a969456a32918b4c6c5cb044210be04de0b97a43d57d90f97a76469e3a9d967522fe30745cea9d2c47d038e48a4fd45009b7af28
537+
checksum: 10c0/e22cdeb77bc8fd4940a6d8b4b87cf4052383531f97108e2f604a13d5a62d126f7e7d3cf0a59c73f5d48733735ad88df6b72f680403c972843df1e061a18a4e64
538538
languageName: node
539539
linkType: hard
540540

0 commit comments

Comments
 (0)