-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add type to importing of Object interfaces
- Loading branch information
Showing
3 changed files
with
78 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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 {} | ||
export class MyObject implements MyObjectInterface {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 = "foo"; | ||
}); |