LoginSignup
0
0

More than 5 years have passed since last update.

OctoberCMSのAjax用関数($.request)をTypeScriptから使う

Last updated at Posted at 2018-10-02

OctoberCMSのJavaScript APIを使用してAjaxコールを呼ぶ際、$.request()を使用するが、TypeScriptから呼ぶとエラーになる。型定義ファイルを入れて解決する話。

TL;TR

下記で説明した内容をnodeパッケージとして利用可能にしたので、下記だけで解決する。

npm install pikanji/oc-framework-ts-types

説明

TypeScriptで$.request()を使用すると不明なプロパティとしてエラーになる。

TS2339: Property 'request' does not exist on type 'JQueryStatic<HTMLElement>'.

原因は、request()メソッドはjQueryにOctoberCMSによって追加されたものであり、jQueryの型定義ファイルに含まれていないためである。

modules/system/assets/js/framework.jsにて、下記のように関数が追加されている。

$.fn.request = function (handler, option) {
    ...
    return new Request($this, handler, options);
};

対策

下記のような型定義ファイルを作成することで解決できる。

interface JQueryStatic {
    request(handler: string, options: any): any;
}

詳細は「jQueryに追加した関数のTypeScript型定義を作成する」に記載した。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0