Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'Type not assignable...' when not supplying every option parameter to constructor when using TypeScript JS file checking #50

Open
luke-m opened this issue Jul 8, 2020 · 0 comments

Comments

@luke-m
Copy link

luke-m commented Jul 8, 2020

We're using TypeScript with JS file checking enabled in our project. What that means is that TypeScript will check JS files for JSDoc definitions and interpret them as types.
scrollbooster's index file contains JSDocs definitions:

image

When I try to create a new ScrollBooster instance with the same options as suggested in the README.md like this:

new ScrollBooster({
    viewport: document.querySelector('.viewport'),
    scrollMode: 'transform'
});

... TypeScript complains about missing parameters:

TS2345: Argument of type '{ viewport: HTMLElement; scrollMode: string; }' is not assignable to parameter of type '{ viewport: Element; content: Element; direction: string; pointerMode: string; scrollMode: string; bounce: boolean; bounceForce: number; friction: number; textSelection: boolean; ... 5 more ...; shouldScroll: Function; }'.
  Type '{ viewport: HTMLElement; scrollMode: string; }' is missing the following properties from type '{ viewport: Element; content: Element; direction: string; pointerMode: string; scrollMode: string; bounce: boolean; bounceForce: number; friction: number; textSelection: boolean; ... 5 more ...; shouldScroll: Function; }': content, direction, pointerMode, bounce, and 9 more.

That's because in the JSDocs comments of ScrollBooster's index file, non-essential parameters are not marked as optional. In JSDocs, you can mark parameters as optional by wrapping the parameter name in brackets ( [] ). When I do that for all the non-essential parameters, TypeScript stops complaining and my example works 🎉 .

@ilyashubin I wanted to open a PR, but I can't seem to create a branch for this repo.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant