fix(napi-derive): invalid TypeScript return type

This commit is contained in:
LongYinan 2022-04-27 18:19:38 +08:00
parent 2fecc109ef
commit 44b4cc34e0
No known key found for this signature in database
GPG key ID: C3666B7FC82ADAD7
6 changed files with 12 additions and 9 deletions

View file

@ -68,5 +68,6 @@ jobs:
run: | run: |
yarn build:test yarn build:test
yarn test --verbose yarn test --verbose
yarn tsc -p examples/napi/tsconfig.json --noEmit
env: env:
RUST_BACKTRACE: 1 RUST_BACKTRACE: 1

View file

@ -181,6 +181,7 @@ static KNOWN_TYPES: Lazy<HashMap<&'static str, &'static str>> = Lazy::new(|| {
("Either5", "{} | {} | {} | {} | {}"), ("Either5", "{} | {} | {} | {} | {}"),
("unknown", "unknown"), ("unknown", "unknown"),
("Null", "null"), ("Null", "null"),
("JsNull", "null"),
("null", "null"), ("null", "null"),
("Symbol", "symbol"), ("Symbol", "symbol"),
("JsSymbol", "symbol"), ("JsSymbol", "symbol"),
@ -262,7 +263,7 @@ pub fn ty_to_ts_type(ty: &Type, is_return_ty: bool, is_struct_field: bool) -> (S
if is_struct_field { if is_struct_field {
arg.to_string() arg.to_string()
} else if is_return_ty { } else if is_return_ty {
format!("{}?", arg) format!("{} | null", arg)
} else { } else {
format!("{} | undefined | null", arg) format!("{} | undefined | null", arg)
}, },

View file

@ -106,7 +106,7 @@ Generated by [AVA](https://avajs.dev).
export function xxh64Alias(input: Buffer): bigint␊ export function xxh64Alias(input: Buffer): bigint␊
export function getMapping(): Record<string, number> export function getMapping(): Record<string, number>
export function sumMapping(nums: Record<string, number>): number␊ export function sumMapping(nums: Record<string, number>): number␊
export function mapOption(val?: number | undefined | null): number? export function mapOption(val?: number | undefined | null): number | null
export function returnNull(): null␊ export function returnNull(): null␊
export function returnUndefined(): void␊ export function returnUndefined(): void␊
export function add(a: number, b: number): number␊ export function add(a: number, b: number): number␊
@ -115,7 +115,7 @@ Generated by [AVA](https://avajs.dev).
export function createObj(): object␊ export function createObj(): object␊
export function getGlobal(): typeof global␊ export function getGlobal(): typeof global␊
export function getUndefined(): void␊ export function getUndefined(): void␊
export function getNull(): JsNull␊ export function getNull(): null␊
export interface AllOptionalObject {␊ export interface AllOptionalObject {␊
name?: string␊ name?: string␊
age?: number␊ age?: number␊
@ -243,7 +243,7 @@ Generated by [AVA](https://avajs.dev).
export type JsAssets = Assets␊ export type JsAssets = Assets␊
export class Assets {␊ export class Assets {␊
constructor()␊ constructor()␊
get(id: number): JsAsset? get(id: number): JsAsset | null
}␊ }␊
export type JsAsset = Asset␊ export type JsAsset = Asset␊
export class Asset {␊ export class Asset {␊

View file

@ -96,7 +96,7 @@ export function tsRename(a: { foo: number }): string[]
export function xxh64Alias(input: Buffer): bigint export function xxh64Alias(input: Buffer): bigint
export function getMapping(): Record<string, number> export function getMapping(): Record<string, number>
export function sumMapping(nums: Record<string, number>): number export function sumMapping(nums: Record<string, number>): number
export function mapOption(val?: number | undefined | null): number? export function mapOption(val?: number | undefined | null): number | null
export function returnNull(): null export function returnNull(): null
export function returnUndefined(): void export function returnUndefined(): void
export function add(a: number, b: number): number export function add(a: number, b: number): number
@ -105,7 +105,7 @@ export function listObjKeys(obj: object): Array<string>
export function createObj(): object export function createObj(): object
export function getGlobal(): typeof global export function getGlobal(): typeof global
export function getUndefined(): void export function getUndefined(): void
export function getNull(): JsNull export function getNull(): null
export interface AllOptionalObject { export interface AllOptionalObject {
name?: string name?: string
age?: number age?: number
@ -233,7 +233,7 @@ export class NinjaTurtle {
export type JsAssets = Assets export type JsAssets = Assets
export class Assets { export class Assets {
constructor() constructor()
get(id: number): JsAsset? get(id: number): JsAsset | null
} }
export type JsAsset = Asset export type JsAsset = Asset
export class Asset { export class Asset {

View file

@ -4,7 +4,8 @@
"compilerOptions": { "compilerOptions": {
"outDir": "./dist", "outDir": "./dist",
"rootDir": "__test__", "rootDir": "__test__",
"target": "ES2018" "target": "ES2018",
"skipLibCheck": false
}, },
"exclude": ["dist", "index.d.ts"] "exclude": ["dist"]
} }