-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Enhance typing of postMessage in Service Workers #25176
Comments
We should be able to strongly type the type Transferable =
| number
| string
| boolean
| null
| undefined
| Date
| ImageData
| Number
| Boolean
| String
| Date
| RegExp
| Blob
| File
| FileList
| ArrayBuffer
| ArrayBufferView
| Map<unknown, unknown>
| Set<unknown>;
declare function postMessage(message: Transferable | Transferable[], targetOrigin?: string, transfer?: any[]): void; As for the side effect tracking, or use after transfer, i am afraid the compiler is not equipped to do this sort of analysis at the time being. |
PRs welcomed! |
Cool! I'd file a PR for 1 and 2 as soon as I have some time. |
The change will need to be done in https://github.com/Microsoft/TSJS-lib-generator |
This issue is only partially fixed by the PR in the TSJS repository. I don't think the |
The PR that closed this did not fix this. The signature still takes |
The PR only fixed the |
Search Terms
Suggestion
The Service Worker APIs come with a mthod
postMessage
(e.g. window.postMessage). Three things:The parameter
message
is processed using the structured clone algorithm, which requires the value tosatisfy certain constrains: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Structured_clone_algorithm
message
:any
message
(or an object containing these values):Error
s andFunction
smessage
type fromany
to something more specific, I don't know if there is a way to constrain the object requirements.any
allows passing not-serializable datamessage
MessageEvent#data
There is a
Transferable
interface which is used to provide transfering objects instead of copying them.ArrayBuffer
,MessagePort
,ImageBitmap
Transferable
is basically an empty interfaceany
allows passing not-transferable datatransfer
/transferList
toArrayBuffer | MessagePort | ImageBitmap
(or find another way of constraining the type)A transfered object cannot be used after it was transferred (e.g. using
postMessage()
)Use Cases
DATA_CLONE_ERR
).Examples
1.:
These errors could be prevented (all valid according to the current definitions):
2.:
This error could be prevented:
3.:
This unintentional use-after-transfer could be prevented / pointed at:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: