Creating an Object implementing a Typescript Interface -
i'm providing public interface data, performance , memory reasons want use object , not class. there's no code on interface, purely data structure.
i want able declare object using interface if change interface it'll show error , doesn't missed.
interface datastructure { key1: string; key2: number; key3: boolean; key4: null; } const defaultdata: {[key: string]: propertydescriptor} = { key1: {writable: true, enumerable: true}, key2: {writable: true, enumerable: true}, key3: {writable: true, enumerable: true} }; var mydata = object.create(null, defaultdata);
i want report error missing key4
in defaultdata
object, can't quite figure out how right now.
(this needed due potentially tens of thousands of objects needing same structure, , not wanting waste gc on them when it's easier re-use them - wanting map stores, no need object prototype chain.)
i think you're looking for:
const defaultdata: {[k in keyof datastructure]: propertydescriptor} = { ... }
the compiler compile saying:
type '{ key1: { writable: true; enumerable: true; }; key2: { writable: true; enumerable: true; }; key3:...' not assignable type '{ key1: propertydescriptor; key2: propertydescriptor; key3: propertydescriptor; key4: propertydes...'.
property 'key4' missing in type '{ key1: { writable: true; enumerable: true; }; key2: { writable: true; enumerable: true; }; key3:...'.
Comments
Post a Comment