mirror of
https://github.com/oxc-project/oxc.git
synced 2026-11-04 10:29:58 +00:00
fix(isolated-declarations): require annotations for satisfies initializers (#22898)
Fixes an isolated declarations gap where exported variables initialized with a `satisfies` expression were being inferred instead of requiring an explicit annotation.
TypeScript reports TS9010 for cases like:
```ts
export const x = { a: 1 } satisfies { a: number };
```
but Oxc previously emitted an inferred declaration. This changes isolated declaration inference so `satisfies` initializers do not unwrap to the inner expression for variable type inference, matching TypeScript’s behavior.
Added a regression fixture covering:
- object literal `satisfies` initializer
- primitive `satisfies` initializer
- explicitly annotated `satisfies` initializer, which should still emit normally
[TypeScript playground](https://www.typescriptlang.org/play/?isolatedDeclarations=true#code/KYDwDg9gTgLgBAYwgOwM7xHAvHA3nAQwC44BGOAXzlQJgEtUAzO4VPQk5AVwFsAjYFEoBuAFBA)
This commit is contained in:
1 parent
44e40fa94c
commit
49e63f77a0
3 files changed
+38
-3
No files matched your search
@@ -64,9 +64,6 @@ impl<'a> IsolatedDeclarations<'a> {
|
||||
Expression::TSNonNullExpression(expr) => {
|
||||
self.infer_type_from_expression(&expr.expression)
|
||||
}
|
||||
Expression::TSSatisfiesExpression(expr) => {
|
||||
self.infer_type_from_expression(&expr.expression)
|
||||
}
|
||||
Expression::UnaryExpression(expr) => {
|
||||
if Self::can_infer_unary_expression(expr) {
|
||||
self.infer_type_from_expression(&expr.argument)
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
export const object = { a: 1 } satisfies { a: number };
|
||||
export const number = 1 satisfies number;
|
||||
export const annotated: { a: number } = { a: 1 } satisfies { a: number };
|
||||
@@ -0,0 +1,35 @@
|
||||
---
|
||||
source: crates/oxc_isolated_declarations/tests/mod.rs
|
||||
input_file: crates/oxc_isolated_declarations/tests/fixtures/satisfies.ts
|
||||
---
|
||||
```
|
||||
==================== .D.TS ====================
|
||||
|
||||
export declare const object: unknown;
|
||||
export declare const number: unknown;
|
||||
export declare const annotated: {
|
||||
a: number;
|
||||
};
|
||||
|
||||
|
||||
==================== Errors ====================
|
||||
|
||||
x TS9010: Variable must have an explicit type annotation with
|
||||
| --isolatedDeclarations.
|
||||
,-[1:14]
|
||||
1 | export const object = { a: 1 } satisfies { a: number };
|
||||
: ^^^^^^
|
||||
2 | export const number = 1 satisfies number;
|
||||
`----
|
||||
|
||||
x TS9010: Variable must have an explicit type annotation with
|
||||
| --isolatedDeclarations.
|
||||
,-[2:14]
|
||||
1 | export const object = { a: 1 } satisfies { a: number };
|
||||
2 | export const number = 1 satisfies number;
|
||||
: ^^^^^^
|
||||
3 | export const annotated: { a: number } = { a: 1 } satisfies { a: number };
|
||||
`----
|
||||
|
||||
|
||||
```
|
||||
Reference in new issue
Block a user