2

There is this library called 'resize-observer-polyfill' which declares that it has a default export:

declare var ResizeObserver: {
  prototype: ResizeObserver;
  new(callback: ResizeObserverCallback): ResizeObserver;
}

export default ResizeObserver;

But I cannot import this library because TS complains.

import ResizeObserver from 'resize-observer-polyfill'

// If I do this instead, TS is happy but ResizeObserver is undefined !!
import * as ResizeObserver from 'resize-observer-polyfill'

TS config extract:

, "compilerOptions":
  { "jsx": "react"
  , "module": "commonjs"
  , "noImplicitAny": true
  , "outDir": "output/dist"
  , "reactNamespace": "JSX"
  , "sourceMap": true
  , "strictNullChecks": true
  , "target": "es6"
  }

Using allowSyntheticDefaultImports makes types and runtime happy but then many imports fail in jest (using ts-jest) unless I set skipBabel to true.

Using the skipBabel option, this import fails in tests with an undefined value, I have to use the import * as ResizeObserver syntax in tests.

What is the proper way to do this ? Why is it such a mess anyway ?

2
  • Try import {ResizeObserver} from 'resize-observer-polyfill' Commented Mar 7, 2018 at 12:28
  • I believe you must declare a module with the same name as the library. Commented Mar 7, 2018 at 12:34

1 Answer 1

1

Try the first syntax (import ResizeObserver from 'resize-observer-polyfill') with allowSyntheticDefaultImports and esModuleInterop both set to true in your tsconfig.json. You need the former to allow import ResizeObserver instead of import * as ResizeObserver, and I think you may need the latter since you're targeting es6.

I found the release notes for TS 2.7 very helpful in understanding this.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.