Skip to content

Commit

Permalink
fix(es/codegen): Emit named type in mapped types (#9038)
Browse files Browse the repository at this point in the history
**Description:**

Fixes the emit of mapped types.
  • Loading branch information
dsherret committed Jun 12, 2024
1 parent ac5eb02 commit 91e92ec
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 6 deletions.
20 changes: 15 additions & 5 deletions crates/swc_ecma_codegen/src/typescript.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,12 +482,14 @@ where
}

punct!("[");

emit!(n.type_param.name);

if n.type_param.constraint.is_some() {
if let Some(constraints) = &n.type_param.constraint {
space!();
keyword!("in");
space!();
emit!(constraints);
}

if let Some(default) = &n.type_param.default {
Expand All @@ -497,7 +499,12 @@ where
emit!(default);
}

emit!(n.type_param.constraint);
if let Some(name_type) = &n.name_type {
space!();
keyword!("as");
space!();
emit!(name_type);
}

punct!("]");

Expand All @@ -518,9 +525,12 @@ where
},
}

punct!(":");
space!();
emit!(n.type_ann);
if let Some(type_ann) = &n.type_ann {
punct!(":");
space!();
emit!(type_ann);
}

formatting_semi!();

self.wr.write_line()?;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type NoTypeAnn = {
[value: string];
};
type MappedTypeWithNamedType = {
[K in T as U]: undefined;
};
type Partial<T> = {
[P in keyof T]?: T[P];
};
type OptionalPlus<T> = {
[P in keyof T]+?: T[P];
};
type OptionalMinus<T> = {
[P in keyof T]-?: T[P];
};
type Flags<T> = {
[P in keyof T]: boolean;
};
type ReadOnly<T> = {
readonly [P in keyof T]: T[P];
};
type ReadOnlyPlus<T> = {
+readonly [P in keyof T]: T[P];
};
type ReadOnlyMinus<T> = {
-readonly [P in keyof T]: T[P];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
type NoTypeAnn = {
[value: string];
};
type MappedTypeWithNamedType = {
[K in T as U]: undefined;
};
type Partial<T> = {
[P in keyof T]?: T[P];
};
type OptionalPlus<T> = {
[P in keyof T]+?: T[P];
};
type OptionalMinus<T> = {
[P in keyof T]-?: T[P];
};
type Flags<T> = {
[P in keyof T]: boolean;
};
type ReadOnly<T> = {
readonly [P in keyof T]: T[P];
};
type ReadOnlyPlus<T> = {
+readonly [P in keyof T]: T[P];
};
type ReadOnlyMinus<T> = {
-readonly [P in keyof T]: T[P];
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
type Lazyify__2<T__3> = {
[K__3 in keyof T__3]: () => T__3[K__3];
[K__3 in keyof T__3 as `get${Capitalize<K__3 & string>}`]: () => T__3[K__3];
};

0 comments on commit 91e92ec

Please sign in to comment.