fix(napi): type constraint for either types
This commit is contained in:
parent
fb8d8b97d6
commit
a5c19ce1da
6 changed files with 73 additions and 93 deletions
examples/napi
|
@ -57,6 +57,8 @@ Generated by [AVA](https://avajs.dev).
|
|||
export function either4(input: string | number | boolean | Obj): number␊
|
||||
export function receiveClassOrNumber(either: number | JsClassForEither): number␊
|
||||
export function receiveMutClassOrNumber(either: number | JsClassForEither): number␊
|
||||
export function returnEitherClass(input: number): number | JsClassForEither␊
|
||||
export function eitherFromOption(): JsClassForEither | undefined␊
|
||||
/** default enum values are continuos i32s start from 0 */␊
|
||||
export const enum Kind {␊
|
||||
/** Barks */␊
|
||||
|
|
Binary file not shown.
|
@ -93,6 +93,8 @@ import {
|
|||
CssStyleSheet,
|
||||
asyncReduceBuffer,
|
||||
callbackReturnPromise,
|
||||
returnEitherClass,
|
||||
eitherFromOption,
|
||||
} from '../'
|
||||
|
||||
test('export const', (t) => {
|
||||
|
@ -467,6 +469,15 @@ test('receive class reference in either', (t) => {
|
|||
t.is(receiveMutClassOrNumber(c), 100)
|
||||
})
|
||||
|
||||
test('return either class', (t) => {
|
||||
t.is(returnEitherClass(1), 1)
|
||||
t.true(returnEitherClass(-1) instanceof JsClassForEither)
|
||||
})
|
||||
|
||||
test('either from option', (t) => {
|
||||
t.true(eitherFromOption() instanceof JsClassForEither)
|
||||
})
|
||||
|
||||
test('either3', (t) => {
|
||||
t.is(either3(2), 2)
|
||||
t.is(either3('hello'), 'hello'.length)
|
||||
|
|
2
examples/napi/index.d.ts
vendored
2
examples/napi/index.d.ts
vendored
|
@ -47,6 +47,8 @@ export interface Obj {
|
|||
export function either4(input: string | number | boolean | Obj): number
|
||||
export function receiveClassOrNumber(either: number | JsClassForEither): number
|
||||
export function receiveMutClassOrNumber(either: number | JsClassForEither): number
|
||||
export function returnEitherClass(input: number): number | JsClassForEither
|
||||
export function eitherFromOption(): JsClassForEither | undefined
|
||||
/** default enum values are continuos i32s start from 0 */
|
||||
export const enum Kind {
|
||||
/** Barks */
|
||||
|
|
|
@ -82,3 +82,17 @@ fn receive_mut_class_or_number(either: Either<u32, &mut JsClassForEither>) -> u3
|
|||
Either::B(_) => 100,
|
||||
}
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn return_either_class(input: i32) -> Either<u32, JsClassForEither> {
|
||||
if input > 0 {
|
||||
Either::A(input as u32)
|
||||
} else {
|
||||
Either::B(JsClassForEither {})
|
||||
}
|
||||
}
|
||||
|
||||
#[napi]
|
||||
fn either_from_option() -> Either<JsClassForEither, Undefined> {
|
||||
Some(JsClassForEither {}).into()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue