Skip to content
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

Make it possible to constrain the property value types of an object to type postMessage() #29941

Closed
5 tasks done
felixfbecker opened this issue Feb 16, 2019 · 5 comments
Closed
5 tasks done
Labels
Duplicate An existing issue was already created

Comments

@felixfbecker
Copy link
Contributor

felixfbecker commented Feb 16, 2019

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:

type Scalar = string | number | boolean | undefined | null
interface CloneableObject {
  [key: string]: Scalar | CloneableObject
}
function postMessage(message: CloneableObject): void

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:

type CloneableObject = {
  [_ in string | number]: Scalar | CloneableObject
}
interface Foo {
  foo: 123
}
const foo: Foo = { foo: 123 }
const cloneable: CloneableObject = foo // Type 'Foo' is not assignable to type CloneableObject'. Index signature is missing in type 'Foo'.

Interestingly, it is assignable when the type is inferred:

const foo = { foo: 123 } // Inferred type: { foo: number; }
const cloneable: CloneableObject = foo

despite hovers not showing an inferred index signature for foo.

One might also try it with generics, but this doesn't work either:

type CloneableObject<T> = {
  [K in keyof T]: Scalar | CloneableObject<T[K]>
}
function postMessage<T>(message: CloneableObject<T>): void
const foo = { method: () => 123 }
postMessage(foo) // No Error?

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:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Related

#25176
microsoft/TypeScript-DOM-lib-generator#534
#1897
#28019

@jack-williams
Copy link
Collaborator

jack-williams commented Feb 16, 2019

In your last example the type for the property method is inferred as {}. I wonder if there would be an error under #27288. I guess not as it only applies to the entire generic; not subcomponents. Would negation types help here?

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Feb 19, 2019
@RyanCavanaugh
Copy link
Member

From a type system perspective, a function is just an object that happens to also be callable (which is also what it is at runtime). You need negated types to solve this in a comprehensive way.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@felixfbecker
Copy link
Contributor Author

@RyanCavanaugh Sorry, of which issue is this a duplicate? I would like to follow that.
How would this look like if TS had negated types?

@simonbuchan
Copy link

More exactly, postMessage needs structured clonable.

Seems to me that either exact types or negated types should work for this, as they seem at least pretty close to equivalent, but both of those have been stuck for years now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

5 participants