Skip to content

Commit

Permalink
Add type to importing of Object interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
jhugman committed Aug 30, 2024
1 parent 4409c6d commit fd61e96
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -204,11 +204,11 @@ impl<'a> TypeRenderer<'a> {
let type_ = self.as_type(external);
let name = type_name(&type_, self).expect("External types should have type names");
match &type_ {
Type::Enum { .. } => self.import_ext(&name, &namespace),
Type::CallbackInterface { .. }
| Type::Object { .. }
| Type::Custom { .. }
| Type::Enum { .. } => self.import_ext(&name, &namespace),
Type::Record { .. } => self.import_ext_type(&name, &namespace),
| Type::Object { .. }
| Type::Record { .. } => self.import_ext_type(&name, &namespace),
_ => unreachable!(),
};
let ffi_converter_name = ffi_converter_name(&type_, self)
Expand Down
34 changes: 34 additions & 0 deletions typescript/tests/exported.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/
*/

export type MyCustomString = string;

export const MyEnum = (() => {
return {
Variant1: class {},
Variant2: class {},
instanceOf: (obj: any): boolean => true,
};
})();
export type MyEnum = InstanceType<
(typeof MyEnum)[keyof Omit<typeof MyEnum, "instanceOf">]
>;

export type MyRecord = {
prop1: string;
prop2: number;
};

export interface MyCallbackInterface {
myMethod(): void;
}

export interface MyObjectInterface {
myForeignMethod(): void;
}
export class MyObject implements MyObjectInterface {
myForeignMethod(): void {}
}
45 changes: 45 additions & 0 deletions typescript/tests/importing.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/
*/

import {
type MyCallbackInterface,
type MyCustomString,
MyEnum,
MyObject,
type MyObjectInterface,
type MyRecord,
} from "./exported";

import { test } from "../testing/asserts";

test("Records imported as type", (t) => {
const record: MyRecord = {
prop1: "string",
prop2: 42,
};
});

test("Enums imported as objects", (t) => {
const enum_: MyEnum = new MyEnum.Variant1();
});

test("Objects interfaces imported as types", (t) => {
// In our generated code, `MyObject` would not be imported into this
// file because all creation happens via the `FfiConverterTypeMyObject`.
const obj: MyObjectInterface = new MyObject();
});

test("Callback interfaces imported as types", (t) => {
class Impl implements MyCallbackInterface {
myMethod(): void {}
}

const cb = new Impl();
});

test("Custom types imported as types", (t) => {
const s: MyCustomString = "string";
});

0 comments on commit fd61e96

Please sign in to comment.