fix(napi-derive-backend): Option value should produce optional types

This commit is contained in:
LongYinan 2021-11-16 13:02:40 +08:00
parent 0e0bfb1c0a
commit b4a8cadb21
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
5 changed files with 61 additions and 34 deletions
examples/napi

View file

@ -7,7 +7,7 @@ export function bigintAdd(a: BigInt, b: BigInt): BigInt
export function createBigInt(): BigInt
export function createBigIntI64(): BigInt
export function getCwd(callback: (arg0: string) => void): void
export function readFile(callback: (arg0: Error | undefined, arg1: string | null) => void): void
export function readFile(callback: (arg0: Error | undefined, arg1?: string | undefined | null) => void): void
export function eitherStringOrNumber(input: string | number): number
export function returnEither(input: number): string | number
export function either3(input: string | number | boolean): number
@ -19,7 +19,7 @@ export enum Kind { Dog = 0, Cat = 1, Duck = 2 }
export enum CustomNumEnum { One = 1, Two = 2, Three = 3, Four = 4, Six = 6, Eight = 8, Nine = 9, Ten = 10 }
export function enumToI32(e: CustomNumEnum): number
export function throwError(): void
export function mapOption(val: number | null): number | null
export function mapOption(val?: number | undefined | null): number | undefined | null
export function add(a: number, b: number): number
export function fibonacci(n: number): number
export function listObjKeys(obj: object): Array<string>
@ -31,8 +31,8 @@ export function asyncPlus100(p: Promise<number>): Promise<number>
interface PackageJson {
name: string
version: string
dependencies: Record<string, any> | null
devDependencies: Record<string, any> | null
dependencies?: Record<string, any> | undefined | null
devDependencies?: Record<string, any> | undefined | null
}
export function readPackageJson(): PackageJson
export function getPackageJsonName(packageJson: PackageJson): string
@ -64,7 +64,7 @@ export class Blake2BKey {
}
export class Context {
maybeNeed?: boolean | undefined | null
constructor()
static withData(data: string): Context
method(): string

View file

@ -79,6 +79,7 @@ impl Blake2bKey {
#[napi]
pub struct Context {
data: String,
pub maybe_need: Option<bool>,
}
// Test for return `napi::Result` and `Result`
@ -88,12 +89,16 @@ impl Context {
pub fn new() -> napi::Result<Self> {
Ok(Self {
data: "not empty".into(),
maybe_need: None,
})
}
#[napi(factory)]
pub fn with_data(data: String) -> Result<Self> {
Ok(Self { data })
Ok(Self {
data,
maybe_need: Some(true),
})
}
#[napi]