mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
fix(isolated-declarations): do not union a undefined when the param type is any or unknown (#5930)
This commit is contained in:
parent
d901772daa
commit
8780c5440f
4 changed files with 10 additions and 2 deletions
|
|
@ -55,7 +55,9 @@ impl<'a> TSType<'a> {
|
||||||
/// Check if type maybe `undefined`
|
/// Check if type maybe `undefined`
|
||||||
pub fn is_maybe_undefined(&self) -> bool {
|
pub fn is_maybe_undefined(&self) -> bool {
|
||||||
match self {
|
match self {
|
||||||
TSType::TSUndefinedKeyword(_) => true,
|
TSType::TSAnyKeyword(_)
|
||||||
|
| TSType::TSUnknownKeyword(_)
|
||||||
|
| TSType::TSUndefinedKeyword(_) => true,
|
||||||
TSType::TSUnionType(un) => un.types.iter().any(Self::is_maybe_undefined),
|
TSType::TSUnionType(un) => un.types.iter().any(Self::is_maybe_undefined),
|
||||||
_ => false,
|
_ => false,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -95,7 +95,7 @@ impl<'a> IsolatedDeclarations<'a> {
|
||||||
if matches!(ts_type, TSType::TSTypeReference(_)) {
|
if matches!(ts_type, TSType::TSTypeReference(_)) {
|
||||||
self.error(implicitly_adding_undefined_to_type(param.span));
|
self.error(implicitly_adding_undefined_to_type(param.span));
|
||||||
} else if !ts_type.is_maybe_undefined() {
|
} else if !ts_type.is_maybe_undefined() {
|
||||||
// union with undefined
|
// union with `undefined`
|
||||||
return self.ast.ts_type_annotation(
|
return self.ast.ts_type_annotation(
|
||||||
SPAN,
|
SPAN,
|
||||||
self.ast.ts_type_union_type(
|
self.ast.ts_type_union_type(
|
||||||
|
|
|
||||||
|
|
@ -26,3 +26,7 @@ export function fooBad([a, b] = [1, 2]): number {
|
||||||
export const fooBad2 = ({a, b} = { a: 1, b: 2 }): number => {
|
export const fooBad2 = ({a, b} = { a: 1, b: 2 }): number => {
|
||||||
return 2;
|
return 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
export function withAny(a: any = 1, b: string): void { }
|
||||||
|
export function withUnknown(a: unknown = 1, b: string): void { }
|
||||||
|
|
|
||||||
|
|
@ -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 fnDeclBad3<T>(p: T, rParam?: T, r2: T): void;
|
||||||
export declare function fooBad(): number;
|
export declare function fooBad(): number;
|
||||||
export declare const fooBad2: () => 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 ====================
|
==================== Errors ====================
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue