Files
sdk/tests/web/js_interop_generic_cast_test.dart
T
Robert Nystrom ba40d9bece Format tests/web/ using the 3.8 formatting style.
Change-Id: I73edeeaf4934899b71d6335dcb4b4b54dce46bae
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/426340
Auto-Submit: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Nate Biggs <natebiggs@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
2025-05-02 20:17:11 -07:00

48 lines
1.1 KiB
Dart

// Copyright (c) 2024, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Tests that type checks work properly for Legacy JS Objects across generic
// boundaries.
import 'dart:html';
import "dart:js" as js;
import 'dart:js_interop';
import 'dart:js_interop_unsafe';
import 'package:js/js.dart' as pkgJs;
class TestGenericClass<X> {
X x;
TestGenericClass(this.x);
directCast(p) {
return p as PackageJSClass<String>;
}
typeArgCast(p) {
return p as X;
}
}
@pkgJs.JS()
class PackageJSClass<T> {
external factory PackageJSClass(T x);
}
void main() {
js.context.callMethod("eval", [
"""
window.PackageJSClass = function PackageJSClass(x) {
this.x = x;
};
""",
]);
var instance = TestGenericClass<PackageJSClass<JSString>>(
PackageJSClass("Hello".toJS),
);
print(instance.directCast(PackageJSClass("Hello".toJS)));
print(instance.typeArgCast(PackageJSClass("Hello".toJS)));
}