You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I would like to restrict the input to a function to objects that can be serialised, and I've got it working OK for types, but not for classes or interfaces: I'm told they don't have an index signature.
The motivation is that the firefox webextension storage API will only serialise certain objects.
A small example:
Code
declarenamespacebrowser.storage{// Non-firefox implementations don't accept all these typestypeStorageValue=string|number|boolean|// ...StorageArray|StorageMap|StorageSet|StorageObject;// The Index signature makes casting to/from classes or interfaces a pain.// Custom types are OK.interfaceStorageObject{[key: string]: StorageValue;}// These have to be interfaces rather than types to avoid a circular// definition of StorageValueinterfaceStorageArrayextendsArray<StorageValue>{}interfaceStorageMapextendsMap<StorageValue,StorageValue>{}interfaceStorageSetextendsSet<StorageValue>{}typeStorageArea={set: (keys: StorageObject)=>Promise<void>,// ...};constsync: StorageArea;}typeA={prop: string[];}interfaceB{prop: string[];}classC{constructor(publicprop: string[]){}}interfaceDextendsbrowser.storage.StorageObject{prop: string[];}consta: A={prop: []}constb: B={prop: []}constc=newC([])constd: D={prop: []}browser.storage.sync.set({ a })// Worksbrowser.storage.sync.set({ b })// Fails: no index signaturebrowser.storage.sync.set({ c })// Fails: no index signaturebrowser.storage.sync.set({ d })// Worksa.foo=1// Property 'foo' does not exist on type 'A'.b.foo=1// Property 'foo' does not exist on type 'B'.c.foo=1// Property 'foo' does not exist on type 'C'.d.foo=1// No error (this is bad)
Errors:
example.ts(51,26): error TS2345: Argument of type '{ b: B; }' is not assignable to parameter of type 'StorageObject'.
Property 'b' is incompatible with index signature.
Type 'B' is not assignable to type 'StorageValue'.
Type 'B' is not assignable to type 'StorageObject'.
Index signature is missing in type 'B'.
example.ts(52,26): error TS2345: Argument of type '{ c: C; }' is not assignable to parameter of type 'StorageObject'.
Property 'c' is incompatible with index signature.
Type 'C' is not assignable to type 'StorageValue'.
Type 'C' is not assignable to type 'StorageObject'.
Index signature is missing in type 'C'.
example.ts(55,3): error TS2339: Property 'foo' does not exist on type 'A'.
example.ts(56,3): error TS2339: Property 'foo' does not exist on type 'B'.
example.ts(57,3): error TS2339: Property 'foo' does not exist on type 'C'.
In addition to this issue, I'd also like to express that instances of a class can be stored (you can't store objects with functions, but prototypes and constructors are ignored).
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.
TypeScript Version: Version 2.7.0-dev.20171112
I would like to restrict the input to a function to objects that can be serialised, and I've got it working OK for
type
s, but not for classes or interfaces: I'm told they don't have an index signature.This is related to issue #1897.
The motivation is that the firefox webextension storage API will only serialise certain objects.
A small example:
Code
Errors:
In addition to this issue, I'd also like to express that instances of a class can be stored (you can't store objects with functions, but prototypes and constructors are ignored).
This bug is tracked on the web-ext-types repo (that I maintain) at https://github.com/kelseasy/web-ext-types/pull/38#issuecomment-343855598
Expected behavior:
Some feature exists to suit the above use case.
Actual behavior:
See example
Thanks for yo time
The text was updated successfully, but these errors were encountered: