Skip to content

Commit 7c8962b

Browse files
authored
Handled conditional types (dart-archive#70)
1 parent 99fc6c6 commit 7c8962b

File tree

3 files changed

+51
-5
lines changed

3 files changed

+51
-5
lines changed

lib/declaration.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -636,8 +636,12 @@ export default class DeclarationTranspiler extends base.TranspilerBase {
636636
this.emit(
637637
'Warning: Mapped types are not supported in Dart. Uses of this type will be replaced by dynamic.');
638638
this.emitNoSpace('\n');
639+
} else if (ts.isConditionalTypeNode(alias.type)) {
640+
this.emit(
641+
'Warning: Conditional types are not supported in Dart. Uses of this type will be replaced by dynamic.');
642+
this.emitNoSpace('\n');
639643
}
640-
this.emit(alias.getText());
644+
this.emitNoSpace(alias.getText());
641645
this.emitNoSpace('\n');
642646
this.exitCodeComment();
643647
this.emitNoSpace('\n');

lib/facade_converter.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -399,11 +399,16 @@ export class FacadeConverter extends base.TranspilerBase {
399399
const indexTypeName = this.generateDartTypeName(node.indexType, addInsideComment(options));
400400

401401
comment = `${objectTypeName}[${indexTypeName}]`;
402-
} else if (ts.isMappedTypeNode(node)) {
402+
} else if (ts.isMappedTypeNode(node) || ts.isConditionalTypeNode(node)) {
403403
name = 'dynamic';
404404
if (ts.isTypeAliasDeclaration(node.parent)) {
405-
// MappedTypeNodes don't contain information about the name or type arguments that were
406-
// passed to the alias, so we have to get that information from the parent
405+
// Declarations of mapped types and conditional types will be printed in a comment elsewhere
406+
// in the file. Upon reaching a node of these kinds, we do not want to re-print the
407+
// declaration. We just want to print the alias name and the type arguments that were
408+
// passed.
409+
// MappedTypeNodes and ConditionalTypeNodes don't contain information about the name or type
410+
// arguments that were passed to the alias, so we have to get that information from the
411+
// parent.
407412
const parent = node.parent;
408413
comment = parent.name.getText();
409414
if (parent.typeParameters && options.resolvedTypeArguments) {

test/type_test.ts

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,51 @@ abstract class Todo {
166166
167167
/*
168168
Warning: Mapped types are not supported in Dart. Uses of this type will be replaced by dynamic.
169-
type ReadonlyTodo = {
169+
type ReadonlyTodo = {
170170
readonly[P in keyof Todo]: Todo[P];
171171
}
172172
*/
173173
@JS()
174174
external dynamic /*ReadonlyTodo*/ get todo;`);
175175
});
176176
});
177+
178+
it('should support conditional types', () => {
179+
expectTranslate(`
180+
type TypeName<T> = T extends string ? "string" :
181+
T extends string ? "string" :
182+
T extends number ? "number" :
183+
T extends boolean ? "boolean" :
184+
T extends undefined ? "undefined" :
185+
T extends Function ? "function" :
186+
"object";
187+
188+
declare var x: TypeName<number>;
189+
declare var y: TypeName<string>;
190+
declare var z: TypeName<boolean>;`)
191+
.to.equal(
192+
`/*Warning: Conditional types are not supported in Dart. Uses of this type will be replaced by dynamic.
193+
type TypeName<T> = T extends string ? "string" :
194+
T extends string ? "string" :
195+
T extends number ? "number" :
196+
T extends boolean ? "boolean" :
197+
T extends undefined ? "undefined" :
198+
T extends Function ? "function" :
199+
"object";
200+
*/
201+
@JS()
202+
external dynamic /*TypeName<num>*/ get x;
203+
@JS()
204+
external set x(dynamic /*TypeName<num>*/ v);
205+
@JS()
206+
external dynamic /*TypeName<String>*/ get y;
207+
@JS()
208+
external set y(dynamic /*TypeName<String>*/ v);
209+
@JS()
210+
external dynamic /*TypeName<bool>*/ get z;
211+
@JS()
212+
external set z(dynamic /*TypeName<bool>*/ v);`);
213+
});
177214
});
178215

179216
describe('type arguments', () => {

0 commit comments

Comments
 (0)