From 44b4cc34e0b6abfeb2611d9978206767fc92e29f Mon Sep 17 00:00:00 2001 From: LongYinan Date: Wed, 27 Apr 2022 18:19:38 +0800 Subject: [PATCH] fix(napi-derive): invalid TypeScript return type --- .github/workflows/test.yaml | 1 + crates/backend/src/typegen.rs | 3 ++- examples/napi/__test__/typegen.spec.ts.md | 6 +++--- examples/napi/__test__/typegen.spec.ts.snap | Bin 2995 -> 2989 bytes examples/napi/index.d.ts | 6 +++--- examples/napi/tsconfig.json | 5 +++-- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index e9918b78..35644da1 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -68,5 +68,6 @@ jobs: run: | yarn build:test yarn test --verbose + yarn tsc -p examples/napi/tsconfig.json --noEmit env: RUST_BACKTRACE: 1 diff --git a/crates/backend/src/typegen.rs b/crates/backend/src/typegen.rs index ba54b16d..ee05d7cc 100644 --- a/crates/backend/src/typegen.rs +++ b/crates/backend/src/typegen.rs @@ -181,6 +181,7 @@ static KNOWN_TYPES: Lazy> = Lazy::new(|| { ("Either5", "{} | {} | {} | {} | {}"), ("unknown", "unknown"), ("Null", "null"), + ("JsNull", "null"), ("null", "null"), ("Symbol", "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 { arg.to_string() } else if is_return_ty { - format!("{}?", arg) + format!("{} | null", arg) } else { format!("{} | undefined | null", arg) }, diff --git a/examples/napi/__test__/typegen.spec.ts.md b/examples/napi/__test__/typegen.spec.ts.md index 0eeafa58..83adb06e 100644 --- a/examples/napi/__test__/typegen.spec.ts.md +++ b/examples/napi/__test__/typegen.spec.ts.md @@ -106,7 +106,7 @@ Generated by [AVA](https://avajs.dev). export function xxh64Alias(input: Buffer): bigint␊ export function getMapping(): Record␊ export function sumMapping(nums: Record): number␊ - export function mapOption(val?: number | undefined | null): number?␊ + export function mapOption(val?: number | undefined | null): number | null␊ export function returnNull(): null␊ export function returnUndefined(): void␊ export function add(a: number, b: number): number␊ @@ -115,7 +115,7 @@ Generated by [AVA](https://avajs.dev). export function createObj(): object␊ export function getGlobal(): typeof global␊ export function getUndefined(): void␊ - export function getNull(): JsNull␊ + export function getNull(): null␊ export interface AllOptionalObject {␊ name?: string␊ age?: number␊ @@ -243,7 +243,7 @@ Generated by [AVA](https://avajs.dev). export type JsAssets = Assets␊ export class Assets {␊ constructor()␊ - get(id: number): JsAsset?␊ + get(id: number): JsAsset | null␊ }␊ export type JsAsset = Asset␊ export class Asset {␊ diff --git a/examples/napi/__test__/typegen.spec.ts.snap b/examples/napi/__test__/typegen.spec.ts.snap index 4c495ff76970564aa522fbec63942d450bd8765a..fdcffa602b5c1174ff084c4527241b75d3b3576b 100644 GIT binary patch literal 2989 zcmV;e3sUq!RzVDo_$%f*>m-4rJL*oWvJcayJXF zu^EvgX<~}QaE7*3uYv9#^tms^?icnGdd>`o9PuK_+XN2G8J_!`>(IZ9hN18;<$kx8Bo7M9GjxsuOTYhan@6p02?r)0>Z-P28oa5|1sQ9vc$NiZQ&7uu2)2(;fm9ClaEBHxRtnt;Ky5`c4(h$)xs4Z|VzFFOP+u%4TbyxJyLf(JkVe^t;$jKB7xMha~L ztBu!oNqz$VhS=m0#LiD(=rCN7M_F)#g`F_ZZSaS&Dm+ z#o&e1$H^;lORgYrk@hRH*(M`!sx)B|R_6~TO9J-zvlP+83vyQ%+%~wXHrY!Ok+`ya zg~Lu5$QHem``HHiy!YhE6Cey3gGRIS3rz!_s^O9ec&cgG*d1(p7UCd^Qw8)ig#n0E zz_f{@9!ru!J9EfjFGvxPeq9abNmS&4a>EAVNaqfZ8{WIH*PQD7Ng^V#tMLwb7;<@E zJjg{GhdfFZD`DaK;o=(XX4O8$5nqWs2Dexr$O~%%y zG%i9$;P7O<(rakk_dE);RgvlrO)ho2K*+8bL$+COXK&tvYfC6i&~=#iJ*wdSh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4hATDJaGEMjMPpw2g{&>ejR!Jr!J>djjO`R z;M-WXi`>dn|#aW5s;#&VT*G=IrN%OJZl34ile4g`SnkcI!yYV2pg+_}%&)2cn zALJiql=+CZiTNNao)up1#BjG%4_hS?rgT<8Dky970?dB|tnj81#uqJ#Un%C%9}>GjUikiF*;M diff --git a/examples/napi/index.d.ts b/examples/napi/index.d.ts index 3269086e..f4539e09 100644 --- a/examples/napi/index.d.ts +++ b/examples/napi/index.d.ts @@ -96,7 +96,7 @@ export function tsRename(a: { foo: number }): string[] export function xxh64Alias(input: Buffer): bigint export function getMapping(): Record export function sumMapping(nums: Record): number -export function mapOption(val?: number | undefined | null): number? +export function mapOption(val?: number | undefined | null): number | null export function returnNull(): null export function returnUndefined(): void export function add(a: number, b: number): number @@ -105,7 +105,7 @@ export function listObjKeys(obj: object): Array export function createObj(): object export function getGlobal(): typeof global export function getUndefined(): void -export function getNull(): JsNull +export function getNull(): null export interface AllOptionalObject { name?: string age?: number @@ -233,7 +233,7 @@ export class NinjaTurtle { export type JsAssets = Assets export class Assets { constructor() - get(id: number): JsAsset? + get(id: number): JsAsset | null } export type JsAsset = Asset export class Asset { diff --git a/examples/napi/tsconfig.json b/examples/napi/tsconfig.json index 4a99370d..cdab7200 100644 --- a/examples/napi/tsconfig.json +++ b/examples/napi/tsconfig.json @@ -4,7 +4,8 @@ "compilerOptions": { "outDir": "./dist", "rootDir": "__test__", - "target": "ES2018" + "target": "ES2018", + "skipLibCheck": false }, - "exclude": ["dist", "index.d.ts"] + "exclude": ["dist"] }