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 4c495ff7..fdcffa60 100644 Binary files a/examples/napi/__test__/typegen.spec.ts.snap and b/examples/napi/__test__/typegen.spec.ts.snap differ 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"] }