fix(isolated-declarations): do not union a undefined when the param type is any or unknown (#5930)

This commit is contained in:
Dunqing 2024-09-20 15:12:01 +00:00
parent d901772daa
commit 8780c5440f
4 changed files with 10 additions and 2 deletions

View file

@ -55,7 +55,9 @@ impl<'a> TSType<'a> {
/// Check if type maybe `undefined`
pub fn is_maybe_undefined(&self) -> bool {
match self {
TSType::TSUndefinedKeyword(_) => true,
TSType::TSAnyKeyword(_)
| TSType::TSUnknownKeyword(_)
| TSType::TSUndefinedKeyword(_) => true,
TSType::TSUnionType(un) => un.types.iter().any(Self::is_maybe_undefined),
_ => false,
}

View file

@ -95,7 +95,7 @@ impl<'a> IsolatedDeclarations<'a> {
if matches!(ts_type, TSType::TSTypeReference(_)) {
self.error(implicitly_adding_undefined_to_type(param.span));
} else if !ts_type.is_maybe_undefined() {
// union with undefined
// union with `undefined`
return self.ast.ts_type_annotation(
SPAN,
self.ast.ts_type_union_type(

View file

@ -26,3 +26,7 @@ export function fooBad([a, b] = [1, 2]): number {
export const fooBad2 = ({a, b} = { a: 1, b: 2 }): number => {
return 2;
}
export function withAny(a: any = 1, b: string): void { }
export function withUnknown(a: unknown = 1, b: string): void { }

View file

@ -14,6 +14,8 @@ export declare function fnDeclBad2<T>(p: T, r2: T): void;
export declare function fnDeclBad3<T>(p: T, rParam?: T, r2: T): void;
export declare function fooBad(): number;
export declare const fooBad2: () => number;
export declare function withAny(a: any, b: string): void;
export declare function withUnknown(a: unknown, b: string): void;
==================== Errors ====================