diff --git a/serde-generate/src/typescript.rs b/serde-generate/src/typescript.rs index 7c2e2b3ad..57a21aa9d 100644 --- a/serde-generate/src/typescript.rs +++ b/serde-generate/src/typescript.rs @@ -90,7 +90,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> { fn generate_container(&mut self, name: &str, container: &ContainerFormat) -> Result<()> { // ENCODE - writeln!(self.out, "export const {name} = {{")?; + writeln!(self.out, "export const {name} = Object.assign(Object.create(null), {{")?; self.out.indent(); writeln!(self.out, "encode(value: {name}, writer = new BincodeWriter()) {{")?; @@ -139,7 +139,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> { ContainerFormat::TupleStruct(inner_types) => { writeln!(self.out, "const value: {name} = {}", self.quote_read_value(&Format::Tuple(inner_types.clone())))?; } - _ => { writeln!(self.out, "const value = {{}} as {name}")?; } + _ => { writeln!(self.out, "const value = Object.create(null) as {name}")?; } } match container { @@ -160,7 +160,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> { writeln!(self.out, "}}")?; // decode end self.out.unindent(); - writeln!(self.out, "}}")?; // object end + writeln!(self.out, "}})")?; // object end Ok(()) } @@ -216,7 +216,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> { writeln!(self.out, r#"case {index}: {{"#)?; self.out.indent(); - writeln!(self.out, r#"value = {{ $: "{}" }} as $t.WrapperOfCase<{}, "{}">"#, variant.name, name, variant.name); + writeln!(self.out, r#"value = Object.assign(Object.create(null), {{ $: "{}" }}) as $t.WrapperOfCase<{}, "{}">"#, variant.name, name, variant.name); match &variant.value { VariantFormat::Unit => { @@ -230,7 +230,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> { writeln!(self.out, "value.{} = {}", variant.name, self.quote_read_value(inner)); } VariantFormat::Struct(fields) => { - writeln!(self.out, r#"value.{var} = {{}} as $t.WrapperOfCase<{name}, "{var}">["{var}"]"#, var = variant.name); + writeln!(self.out, r#"value.{var} = Object.create(null) as $t.WrapperOfCase<{name}, "{var}">["{var}"]"#, var = variant.name); for field in fields { writeln!(self.out, "value.{}.{} = {}", variant.name, field.name, self.quote_read_value(&field.value))?; } @@ -253,7 +253,7 @@ impl<'a, T: Write> TypeScriptEmitter<'a, T> { writeln!(self.out, "}}")?; // decode end self.out.unindent(); - writeln!(self.out, "}}")?; // object end + writeln!(self.out, "}})")?; // object end Ok(()) } diff --git a/suite/typescript/ts/bincode/registry.ts b/suite/typescript/ts/bincode/registry.ts index dcff7a363..f863fd33a 100644 --- a/suite/typescript/ts/bincode/registry.ts +++ b/suite/typescript/ts/bincode/registry.ts @@ -28,7 +28,7 @@ export type TupleStruct = [$t.i32, $t.f64, $t.str] export type UnitStruct = $t.unit -export const ComplexStruct = { +export const ComplexStruct = Object.assign(Object.create(null), { encode(value: ComplexStruct, writer = new BincodeWriter()) { SimpleStruct.encode(value.inner, writer) writer.writeBool(value.flag) @@ -43,7 +43,7 @@ export const ComplexStruct = { return writer.getBytes() }, decode(input: Uint8Array, reader = new BincodeReader(input)) { - const value = {} as ComplexStruct + const value = Object.create(null) as ComplexStruct value.inner = SimpleStruct.decode(input, reader) value.flag = reader.readBool() value.items = reader.readList(() => MultiEnum.decode(input, reader)) @@ -53,9 +53,9 @@ export const ComplexStruct = { value.map = reader.readMap<$t.i32, $t.i64>(reader.readI32.bind(reader), reader.readI64.bind(reader)) return value } -} +}) -export const MultiEnum = { +export const MultiEnum = Object.assign(Object.create(null), { encode(value: MultiEnum, writer = new BincodeWriter()) { switch (value.$) { case "VariantA": { @@ -86,24 +86,24 @@ export const MultiEnum = { let value: MultiEnum switch (reader.readVariantIndex()) { case 0: { - value = { $: "VariantA" } as $t.WrapperOfCase + value = Object.assign(Object.create(null), { $: "VariantA" }) as $t.WrapperOfCase value.VariantA = reader.readI32() break } case 1: { - value = { $: "VariantB" } as $t.WrapperOfCase + value = Object.assign(Object.create(null), { $: "VariantB" }) as $t.WrapperOfCase value.VariantB = reader.readString() break } case 2: { - value = { $: "VariantC" } as $t.WrapperOfCase - value.VariantC = {} as $t.WrapperOfCase["VariantC"] + value = Object.assign(Object.create(null), { $: "VariantC" }) as $t.WrapperOfCase + value.VariantC = Object.create(null) as $t.WrapperOfCase["VariantC"] value.VariantC.x = reader.readU8() value.VariantC.y = reader.readF64() break } case 3: { - value = { $: "UnitVariant" } as $t.WrapperOfCase + value = Object.assign(Object.create(null), { $: "UnitVariant" }) as $t.WrapperOfCase value.UnitVariant = reader.readUnit() break } @@ -111,9 +111,9 @@ export const MultiEnum = { return value } -} +}) -export const NewtypeStruct = { +export const NewtypeStruct = Object.assign(Object.create(null), { encode(value: NewtypeStruct, writer = new BincodeWriter()) { writer.writeI32(value) return writer.getBytes() @@ -122,23 +122,23 @@ export const NewtypeStruct = { const value: NewtypeStruct = reader.readI32() return value } -} +}) -export const SimpleStruct = { +export const SimpleStruct = Object.assign(Object.create(null), { encode(value: SimpleStruct, writer = new BincodeWriter()) { writer.writeU32(value.a) writer.writeString(value.b) return writer.getBytes() }, decode(input: Uint8Array, reader = new BincodeReader(input)) { - const value = {} as SimpleStruct + const value = Object.create(null) as SimpleStruct value.a = reader.readU32() value.b = reader.readString() return value } -} +}) -export const TupleStruct = { +export const TupleStruct = Object.assign(Object.create(null), { encode(value: TupleStruct, writer = new BincodeWriter()) { writer.writeI32(value[0]) writer.writeF64(value[1]) @@ -149,9 +149,9 @@ export const TupleStruct = { const value: TupleStruct = [reader.readI32(), reader.readF64(), reader.readString()] return value } -} +}) -export const UnitStruct = { +export const UnitStruct = Object.assign(Object.create(null), { encode(value: UnitStruct, writer = new BincodeWriter()) { writer.writeUnit(null) return writer.getBytes() @@ -160,4 +160,4 @@ export const UnitStruct = { const value: $t.unit = reader.readUnit() return value } -} +}) diff --git a/suite/typescript/ts/data.ts b/suite/typescript/ts/data.ts index e756b4ad2..9907a27c4 100644 --- a/suite/typescript/ts/data.ts +++ b/suite/typescript/ts/data.ts @@ -1,29 +1,39 @@ import * as Registry from "./bincode/registry.ts" -export const SimpleStruct_obj: Registry.SimpleStruct = { a: 42, b: "Hello" } +declare global { + interface Object { + dict(): T + } +} +Object.prototype.dict = function(this: T) { + return Object.assign(Object.create(null), this) as T +} + +export const SimpleStruct_obj: Registry.SimpleStruct = { a: 42, b: "Hello" }.dict() export const SimpleStruct_bin = Uint8Array.from([42, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 72, 101, 108, 108, 111]) export const MultiEnum_VariantC_obj: Registry.MultiEnum = { $: "VariantC", - VariantC: { x: 5, y: 3.14 } -} + VariantC: { x: 5, y: 3.14 }.dict() +}.dict() + export const MultiEnum_VariantC_bin = Uint8Array.from([2, 0, 0, 0, 5, 31, 133, 235, 81, 184, 30, 9, 64]) -export const MultiEnum_Unit_obj: Registry.MultiEnum = { $: "UnitVariant", UnitVariant: null } +export const MultiEnum_Unit_obj: Registry.MultiEnum = { $: "UnitVariant", UnitVariant: null }.dict() export const MultiEnum_Unit_bin = Uint8Array.from([3, 0, 0, 0]) export const ComplexStruct_obj: Registry.ComplexStruct = { - inner: { a: 42, b: "Hello" }, + inner: { a: 42, b: "Hello" }.dict(), flag: true, items: [ - { $: "VariantA", VariantA: 10 }, - { $: "VariantB", VariantB: "World" } + { $: "VariantA", VariantA: 10 }.dict(), + { $: "VariantB", VariantB: "World" }.dict() ], unit: null, newtype: 99, tuple: [123, 45.67, "Test"], map: new Map().set(3, 7n) -} +}.dict() export const ComplexStruct_bin = Uint8Array.from([ 42, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 72, 101, 108, 108, 111, 1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 0, 0, 0, 1, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 87, 111, 114, 108, 100, 99, 0, 0, 0, 123, 0, 0, 0, 246, 40, 92, 143, 194, 213, 70, 64, 4, 0, 0, 0, 0, 0, 0, 0, 84, 101, 115, 116, 1, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 7, 0, 0, 0, 0, 0, 0, 0 ]) \ No newline at end of file diff --git a/suite/typescript/tsconfig.json b/suite/typescript/tsconfig.json index e47bef4d6..b30bb7eb0 100644 --- a/suite/typescript/tsconfig.json +++ b/suite/typescript/tsconfig.json @@ -1,5 +1,6 @@ { "compilerOptions": { + "strict": false, "noEmit": true, "module": "ESNext", "moduleResolution": "Bundler", diff --git a/tsconfig.json b/tsconfig.json deleted file mode 100644 index f6c8e4829..000000000 --- a/tsconfig.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "compilerOptions": { - "noEmit": true, - "target": "ESNext", - "module": "Preserve", - "downlevelIteration": true, - "allowImportingTsExtensions": true, - "lib": ["DOM", "ES2023"] - }, - "exclude": [ - "node_modules" - ] -} \ No newline at end of file