Skip to content

[interop] Support typeof type declarations #417

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions web_generator/lib/src/ast/declarations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import 'package:code_builder/code_builder.dart';

import '../interop_gen/namer.dart';
import 'base.dart';
import 'builtin.dart';
import 'helpers.dart';
import 'types.dart';

Expand Down Expand Up @@ -52,6 +53,12 @@ class VariableDeclaration extends NamedDeclaration

@override
String? get dartName => null;

@override
ReferredType<VariableDeclaration> asReferredType([List<Type>? typeArgs]) {
return ReferredType<VariableDeclaration>.fromType(type, this,
typeParams: typeArgs ?? []);
}
}

enum VariableModifier { let, $const, $var }
Expand Down Expand Up @@ -115,6 +122,14 @@ class FunctionDeclaration extends NamedDeclaration
..requiredParameters.addAll(requiredParams)
..optionalParameters.addAll(optionalParams));
}

@override
ReferredType<FunctionDeclaration> asReferredType([List<Type>? typeArgs]) {
// TODO: We could do better here and make the function type typed
return ReferredType<FunctionDeclaration>.fromType(
BuiltinType.referred('Function', typeParams: typeArgs ?? [])!, this,
typeParams: typeArgs ?? []);
}
}

class ParameterDeclaration {
Expand Down
19 changes: 19 additions & 0 deletions web_generator/lib/src/ast/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'base.dart';
import 'builtin.dart';
import 'declarations.dart';

/// A type referring to a type in the TypeScript AST
class ReferredType<T extends Declaration> extends Type {
@override
String name;
Expand All @@ -24,6 +25,9 @@ class ReferredType<T extends Declaration> extends Type {
required this.declaration,
this.typeParams = const []});

factory ReferredType.fromType(Type type, T declaration,
{List<Type> typeParams}) = ReferredDeclarationType;

@override
Reference emit([TypeOptions? options]) {
// TODO: Support referred types imported from URL
Expand All @@ -34,6 +38,21 @@ class ReferredType<T extends Declaration> extends Type {
}
}

class ReferredDeclarationType<T extends Declaration> extends ReferredType<T> {
Type type;

@override
String get name => type.name ?? declaration.name;

ReferredDeclarationType(this.type, T declaration, {super.typeParams})
: super(name: declaration.name, declaration: declaration);

@override
Reference emit([covariant TypeOptions? options]) {
return type.emit(options);
}
}

// TODO(https://github.com/dart-lang/web/issues/385): Implement Support for UnionType (including implementing `emit`)
class UnionType extends Type {
final List<Type> types;
Expand Down
31 changes: 31 additions & 0 deletions web_generator/lib/src/interop_gen/transform/transformer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,37 @@ class Transformer {
_ => throw UnimplementedError(
'Unsupported Literal Kind ${literal.kind}')
});
case TSSyntaxKind.TypeQuery:
final typeQuery = type as TSTypeQueryNode;

// TODO(nikeokoronkwo): Refactor this once #402 lands, https://github.com/dart-lang/web/pull/415
final exprName = typeQuery.exprName;
final name = exprName.text;
final typeArguments = typeQuery.typeArguments?.toDart;

var declarationsMatching = nodeMap.findByName(name);
if (declarationsMatching.isEmpty) {
final symbol = typeChecker.getSymbolAtLocation(exprName);
final declarations = symbol?.getDeclarations();
final declaration = declarations?.toDart.first;

if (declaration == null) {
throw Exception('Found no declaration matching $name');
}

transform(declaration);

declarationsMatching = nodeMap.findByName(name);
}

final firstNode =
declarationsMatching.whereType<NamedDeclaration>().first;

return firstNode.asReferredType(
(typeArguments ?? [])
.map((type) => _transformType(type, typeArg: true))
.toList(),
);
case TSSyntaxKind.ArrayType:
return BuiltinType.primitiveType(PrimitiveType.array, typeParams: [
getJSTypeAlternative(
Expand Down
16 changes: 16 additions & 0 deletions web_generator/lib/src/js/typescript.types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ extension type const TSSyntaxKind._(num _) {
static const TSSyntaxKind TypeReference = TSSyntaxKind._(183);
static const TSSyntaxKind ArrayType = TSSyntaxKind._(188);
static const TSSyntaxKind LiteralType = TSSyntaxKind._(201);
static const TSSyntaxKind TypeQuery = TSSyntaxKind._(186);

/// Other
static const TSSyntaxKind Identifier = TSSyntaxKind._(80);
Expand Down Expand Up @@ -102,6 +103,21 @@ extension type TSUnionTypeNode._(JSObject _) implements TSTypeNode {
external TSNodeArray<TSTypeNode> get types;
}

// TODO(nikeokoronkwo): Implements TSNodeWithTypeArguments
// once #402 and #409 are closed
@JS('TypeQueryNode')
extension type TSTypeQueryNode._(JSObject _) implements TSTypeNode {
@redeclare
TSSyntaxKind get kind => TSSyntaxKind.TypeQuery;

// TODO(nikeokoronkwo): Change to EntityName to support
// qualified names, https://github.com/dart-lang/web/issues/416
external TSIdentifier get exprName;
external TSNodeArray<TSTypeNode>? get typeArguments;
}

// TODO(nikeokoronkwo): Implements TSNodeWithTypeArguments
// once #402 and #409 are closed
@JS('TypeReferenceNode')
extension type TSTypeReferenceNode._(JSObject _) implements TSTypeNode {
@redeclare
Expand Down
36 changes: 36 additions & 0 deletions web_generator/test/integration/interop_gen/ts_typing_expected.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// ignore_for_file: constant_identifier_names, non_constant_identifier_names

// ignore_for_file: no_leading_underscores_for_library_prefixes
import 'dart:js_interop' as _i1;

@_i1.JS()
external String get myString;
@_i1.JS()
external _i1.JSArray<_i1.JSNumber> get myNumberArray;
@_i1.JS()
external String get myCloneString;
@_i1.JS()
external _i1.JSArray<_i1.JSArray<_i1.JSNumber>> get muCloneNumberArray;
extension type const MyEnum._(int _) {
static const MyEnum A = MyEnum._(0);

static const MyEnum B = MyEnum._(1);

static const MyEnum C = MyEnum._(2);

static const MyEnum D = MyEnum._(4);
}
@_i1.JS()
external MyEnum get myEnumValue;
@_i1.JS()
external MyEnum get myEnumValue2;
@_i1.JS()
external String myFunction(String param);
@_i1.JS()
external _i1.JSFunction myFunctionAlias;
@_i1.JS()
external _i1.JSFunction myFunctionAlias2;
@_i1.JS()
external String myEnclosingFunction(_i1.JSFunction func);
@_i1.JS()
external _i1.JSFunction get myEnclosingFunctionAlias;
17 changes: 17 additions & 0 deletions web_generator/test/integration/interop_gen/ts_typing_input.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
export declare const myString: string;
export declare const myNumberArray: number[];
export declare const myCloneString: typeof myString;
export declare const muCloneNumberArray: typeof myNumberArray[];
export declare enum MyEnum {
A = 0,
B = 1,
C = 2,
D = 4
}
export declare const myEnumValue: MyEnum;
export declare const myEnumValue2: typeof MyEnum;
export declare function myFunction(param: string): string;
export declare let myFunctionAlias: typeof myFunction;
export declare let myFunctionAlias2: typeof myFunctionAlias;
export declare function myEnclosingFunction(func: typeof myFunction): string;
export declare const myEnclosingFunctionAlias: typeof myEnclosingFunction;