diff --git a/types/jquery/index.d.ts b/types/jquery/index.d.ts index 4a0b3c30f1..d9d5a5bdc6 100644 --- a/types/jquery/index.d.ts +++ b/types/jquery/index.d.ts @@ -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 { * @since 1.0 */ (selector: JQuery.Selector, context: Element | Document | JQuery): JQuery; + // 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. + (window: Window, discriminator: boolean): JQueryStatic; /** * Creates DOM elements on the fly from the provided string of raw HTML. * diff --git a/types/jquery/jquery-tests.ts b/types/jquery/jquery-tests.ts index 4ebc7b9fe1..c31352cb02 100644 --- a/types/jquery/jquery-tests.ts +++ b/types/jquery/jquery-tests.ts @@ -1,4 +1,9 @@ function JQuery() { + function type_assertion() { + const $el = $(document.createElement('canvas')); + const $canvas = $el as JQuery; + } + 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; + } + function type_annotation() { const jq: JQueryStatic = $; } diff --git a/types/jquery/test/jquery-no-window-module-tests.ts b/types/jquery/test/jquery-no-window-module-tests.ts index 700be9d60c..2f2de4cc98 100644 --- a/types/jquery/test/jquery-no-window-module-tests.ts +++ b/types/jquery/test/jquery-no-window-module-tests.ts @@ -1,8 +1,5 @@ import jQueryFactory = require('jquery'); -const jq = jQueryFactory(window); -// ExpectType will report 'jq' as 'JQueryStatic & JQuery' even though the compiler seems to know that 'jq' is 'JQueryStatic' -// // $ExpectType JQueryStatic -// jq; -// jq === jQuery(); -jq === jQuery; +const jq = jQueryFactory(window, true); +// $ExpectType JQueryStatic +jq; diff --git a/types/jquery/test/jquery-slim-no-window-module-tests.ts b/types/jquery/test/jquery-slim-no-window-module-tests.ts index fdbd34e06c..2e104af0eb 100644 --- a/types/jquery/test/jquery-slim-no-window-module-tests.ts +++ b/types/jquery/test/jquery-slim-no-window-module-tests.ts @@ -1,8 +1,5 @@ import jQueryFactory = require('jquery/dist/jquery.slim'); -const jq = jQueryFactory(window); -// ExpectType will report 'jq' as 'JQueryStatic & JQuery' even though the compiler seems to know that 'jq' is 'JQueryStatic' -// // $ExpectType JQueryStatic -// jq; -// jq === jQuery(); -jq === jQuery; +const jq = jQueryFactory(window, true); +// $ExpectType JQueryStatic +jq; diff --git a/types/jquery/test/jquery-slim-window-module-tests.ts b/types/jquery/test/jquery-slim-window-module-tests.ts index 8072811f08..76870e7a04 100644 --- a/types/jquery/test/jquery-slim-window-module-tests.ts +++ b/types/jquery/test/jquery-slim-window-module-tests.ts @@ -1,8 +1,5 @@ import * as jq from 'jquery/dist/jquery.slim'; const $window = jq(window); -// ExpectType will report 'jq' as 'JQueryStatic & JQuery' even though the compiler seems to know that 'jq' is 'JQuery' -// // $ExpectType JQuery -// $window; -// $window === jq; -$window === jq(); +// $ExpectType JQuery +$window; diff --git a/types/jquery/test/jquery-window-module-tests.ts b/types/jquery/test/jquery-window-module-tests.ts index 85f2c4c4ea..71e20bc4a0 100644 --- a/types/jquery/test/jquery-window-module-tests.ts +++ b/types/jquery/test/jquery-window-module-tests.ts @@ -1,8 +1,18 @@ import * as jq from 'jquery'; const $window = jq(window); -// ExpectType will report 'jq' as 'JQueryStatic & JQuery' even though the compiler seems to know that 'jq' is 'JQuery' -// // $ExpectType JQuery -// $window; -// $window === jq; -$window === jq(); +// $ExpectType JQuery +$window; + +class CanvasLayersDirective { + private readonly $renderingCanvas: JQuery; + private readonly $offscreenCanvas: JQuery; + + 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; + this.$renderingCanvas = $Canvas(elementRef.nativeElement); + this.$offscreenCanvas = $Canvas(document.createElement('canvas')); + } +}