Skip to content

Commit

Permalink
fix(isolated-declarations): do not union a undefined when the param t…
Browse files Browse the repository at this point in the history
…ype is any or unknown
  • Loading branch information
Dunqing authored and Boshen committed Sep 20, 2024
1 parent f07ff14 commit e32820d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 2 deletions.
4 changes: 3 additions & 1 deletion crates/oxc_ast/src/ast_impl/ts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion crates/oxc_isolated_declarations/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 { }
Original file line number Diff line number Diff line change
Expand Up @@ -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 ====================
Expand Down

0 comments on commit e32820d

Please sign in to comment.