Make it possible to constrain the property value types of an object to type postMessage() #29941
Closed
5 tasks done
Labels
Duplicate
An existing issue was already created
Search Terms
postMessage object value constraint
Suggestion
Currently, it is not possible to define a constraint for the value types an object is allowed to have. Many people think this would work:
But it will reject any object that do not define an index signature - which makes sense, because this is the index signature syntax.
Surprisingly, it is also not possible with mapped types when the value is typed with an interface or class:
Interestingly, it is assignable when the type is inferred:
despite hovers not showing an inferred index signature for
foo
.One might also try it with generics, but this doesn't work either:
Use Cases
postMessage()
throws an error at runtime when passing it an object that has a method on it. This error should be caught at compile time. This is even more important when using proxy abstractions that build on top of postMessage(), like comlink, because it can be hard to spot at which point a function accepts only cloneable values through multiple layers of abstraction. This constraint should flow through the type system.As using webworkers, service workers, worker_threads in Node etc is more popular this is an annoying gap in the type system.
Checklist
My suggestion meets these guidelines:
Related
#25176
microsoft/TypeScript-DOM-lib-generator#534
#1897
#28019
The text was updated successfully, but these errors were encountered: