LoginSignup
6
2

More than 3 years have passed since last update.

stickyfilljs を使おうとしたら TypeScript に怒られて dom.iterable の存在を初めて知る

Last updated at Posted at 2019-06-05

スクロールに追従してくれる便利プロパティ position: sticky 。これを IE11 でも使えるように wilddeer/stickyfill をインストールしました。

README の通りにやってみる。

sample.ts
import * as Stickyfill from 'stickyfilljs'
const elements: NodeListOf<HTMLElement> = document.querySelectorAll('.sticky');
Stickyfill.add(elements);

するとこんなエラーが出てきます。

Argument of type 'NodeListOf<HTMLElement>' is not assignable to parameter of type 'SingleOrMany<HTMLElement>'.
  Property '[Symbol.iterator]' is missing in type 'NodeListOf<HTMLElement>' but required in type 'Iterable<HTMLElement>'.ts(2345)

え?NodeList は Iterable じゃない…? for 文とか普通に回せると思うんだけど…。

色々調べて識者にも聞いてやっとわかったのは、stickyfill の仕様ではなく TypeScript のコンパイルオプションが足らなかったのが原因だった模様。

tsconfig.json の lib の項目に dom.iterable を追加しないと正しくコンパイルしてくれないっぽい。マジはまったわ。

tsconfig.json
{
  "compilerOptions": {
    "lib": ["es6", "dom", "dom.iterable"]
    },
  }
}

同じような現象に遭遇した方の参考になれば…。

参考文献

6
2
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
6
2