[jquery] Fix issues with importing.

Fix the return type of the factory function.
Fix error with type assertions on JQueryStatic when importing jQuery.
This commit is contained in:
Leonard Thieu
2017-06-21 13:02:58 -04:00
parent 2ab69f8719
commit aa437becb7
6 changed files with 37 additions and 30 deletions

View File

@@ -25,17 +25,11 @@
// TypeScript Version: 2.3
declare module 'jquery' {
function factory(window: Window): JQueryStatic & JQuery;
const factoryOrJQuery: typeof factory & JQueryStatic;
export = factoryOrJQuery;
export = jQuery;
}
declare module 'jquery/dist/jquery.slim' {
function factory(window: Window): JQueryStatic & JQuery;
const factoryOrJQuery: typeof factory & JQueryStatic;
export = factoryOrJQuery;
export = jQuery;
}
declare const jQuery: JQueryStatic;
@@ -2477,6 +2471,9 @@ interface JQueryStatic<TElement extends Node = HTMLElement> {
* @since 1.0
*/
(selector: JQuery.Selector, context: Element | Document | JQuery): JQuery<TElement>;
// HACK: This is the factory function returned when importing jQuery without a DOM. Declaring it separately breaks using the type parameter on JQueryStatic.
// HACK: The discriminator parameter handles the edge case of passing a Window object to JQueryStatic. It doesn't actually exist on the factory function.
<FElement extends Node = HTMLElement>(window: Window, discriminator: boolean): JQueryStatic<FElement>;
/**
* Creates DOM elements on the fly from the provided string of raw HTML.
*

View File

@@ -1,4 +1,9 @@
function JQuery() {
function type_assertion() {
const $el = $(document.createElement('canvas'));
const $canvas = $el as JQuery<HTMLCanvasElement>;
}
function iterable() {
for (const a of $('div')) {
a.textContent = 'myDiv';
@@ -2401,6 +2406,10 @@ function JQuery() {
}
function JQueryStatic() {
function type_assertion() {
const $Canvas = $ as JQueryStatic<HTMLCanvasElement>;
}
function type_annotation() {
const jq: JQueryStatic = $;
}

View File

@@ -1,8 +1,5 @@
import jQueryFactory = require('jquery');
const jq = jQueryFactory(window);
// ExpectType will report 'jq' as 'JQueryStatic<HTMLElement> & JQuery<HTMLElement>' even though the compiler seems to know that 'jq' is 'JQueryStatic<HTMLElement>'
// // $ExpectType JQueryStatic<HTMLElement>
// jq;
// jq === jQuery();
jq === jQuery;
const jq = jQueryFactory(window, true);
// $ExpectType JQueryStatic<HTMLElement>
jq;

View File

@@ -1,8 +1,5 @@
import jQueryFactory = require('jquery/dist/jquery.slim');
const jq = jQueryFactory(window);
// ExpectType will report 'jq' as 'JQueryStatic<HTMLElement> & JQuery<HTMLElement>' even though the compiler seems to know that 'jq' is 'JQueryStatic<HTMLElement>'
// // $ExpectType JQueryStatic<HTMLElement>
// jq;
// jq === jQuery();
jq === jQuery;
const jq = jQueryFactory(window, true);
// $ExpectType JQueryStatic<HTMLElement>
jq;

View File

@@ -1,8 +1,5 @@
import * as jq from 'jquery/dist/jquery.slim';
const $window = jq(window);
// ExpectType will report 'jq' as 'JQueryStatic<HTMLElement> & JQuery<HTMLElement>' even though the compiler seems to know that 'jq' is 'JQuery<HTMLElement>'
// // $ExpectType JQuery<HTMLElement>
// $window;
// $window === jq;
$window === jq();
// $ExpectType JQuery<HTMLElement>
$window;

View File

@@ -1,8 +1,18 @@
import * as jq from 'jquery';
const $window = jq(window);
// ExpectType will report 'jq' as 'JQueryStatic<HTMLElement> & JQuery<HTMLElement>' even though the compiler seems to know that 'jq' is 'JQuery<HTMLElement>'
// // $ExpectType JQuery<HTMLElement>
// $window;
// $window === jq;
$window === jq();
// $ExpectType JQuery<HTMLElement>
$window;
class CanvasLayersDirective {
private readonly $renderingCanvas: JQuery<HTMLCanvasElement>;
private readonly $offscreenCanvas: JQuery<HTMLCanvasElement>;
constructor(elementRef: { nativeElement: any; }) {
// This type assertion results in an error when exporting 'typeof factory & JQueryStatic' where
// 'factory' is jQuery's factory function.
const $Canvas = $ as JQueryStatic<HTMLCanvasElement>;
this.$renderingCanvas = $Canvas(elementRef.nativeElement);
this.$offscreenCanvas = $Canvas(document.createElement('canvas'));
}
}