为什么 TypeScript 告诉我 ” ‘offset’ 在类型 ‘UseScrollOptions’ 中不存在 “?

huangapple go评论54阅读模式
英文:

Why is Typescript telling me " 'offset' does not exist in type 'UseScrollOptions' "?

问题

我正在使用NextJS 13、Typescript和Framer Motion构建React网站。我有以下代码,基本上是从Framer文档中复制并粘贴的:

const { scrollYProgress } = useScroll({
    target: targetRef,
    container: containerRef,
    offset: ['start end', 'end end'],
});

Typescript会将'offset'下划线化,并告诉我:

'offset'在类型'UseScrollOptions'中不存在

当文档告诉你这就是做法时,为什么UseScrollOptions中不存在'type'呢?我以前使用这种滚动创建动画的方法,从来没有遇到过这个错误。我做错了什么?

我尝试卸载并重新安装Framer Motion(我使用的是10.13版本)。我甚至尝试回滚到10.12版本,但结果一样。当我从文档中复制并粘贴代码时,它不起作用,我不知道如何解决这个问题。

英文:

I am building a React website with NextJS 13, Typescript, and Framer Motion. I have the following code, basically copied and pasted from the Framer docs:

const { scrollYProgress } = useScroll({
target: targetRef,
container: containerRef,
offset: ['start end', 'end end'],
});

Typescript is underlining 'offset' and telling me

'offset' does not exist in type 'UseScrollOptions'

How could type offset not exist in UseScrollOptions when the docs tell you that's how to do it? I've used this method of creating animations with scroll before and I've never received this error. What am I doing wrong?

I tried uninstalling and reinstalling Framer motion (I am using 10.13). I even tried rolling back to 10.12 and it gave me the same answer. I have no idea how to fix this when I am copying and pasting from the docs and it isn't working.

答案1

得分: 2

与 framer-motion 10.13 存在相同问题。正如其他答案中指出的那样,看起来当前的类型文件实际上不提供 offset 字段。O.o

类型:UseScrollOptions,扩展自 ScrollOptions

降级到版本 10.12.18 是我找到的唯一解决方案:

npm install framer-motion@10.12.18

注意:在我的情况下,我在 package.json 中使用了 "framer-motion": "^10.12.18"(带有插入符号)允许安装 10.13 版本。要显示当前安装的版本,请使用 npm list

英文:

Having the same issue with framer-motion 10.13. As pointed out in the other answer, it looks like the current type files actually don't provide the offset field. O.o

Types: UseScrollOptions, extends ScrollOptions.

Downgrading to version 10.12.18 was the only solution I found:

npm install framer-motion@10.12.18

Note: In my scenario, I had "framer-motion": "^10.12.18" (with caret) in my package.json which allowed the installation of 10.13. To show the currently installed version use npm list.

答案2

得分: 0

奇怪的是,似乎它没有被定义为 'framer-motion' 包中的一种类型

这里定义了接口 UseScrollOptions,但它没有为 'offset' 定义类型:第 10 行
https://github.com/framer/motion/blob/main/packages/framer-motion/src/value/use-scroll.ts

而它扩展的 ScrollOptions 也没有定义:第 3 行
https://github.com/framer/motion/blob/main/packages/framer-motion/src/render/dom/scroll/types.ts

可能的解决方案是创建一个修复的 PR 提交到他们的仓库。

英文:

Oddly enough, it seems like it has not been defined as a type in the 'framer-motion' package

Here the interface UseScrollOptions is defined and it does not define a type for 'offset': line 10
https://github.com/framer/motion/blob/main/packages/framer-motion/src/value/use-scroll.ts

And neither does ScrollOptions that it extends: line 3
https://github.com/framer/motion/blob/main/packages/framer-motion/src/render/dom/scroll/types.ts

Possible solution would be to create a PR to their repo with a fix.

答案3

得分: -1

你必须已经定义了一个名为UseScrollOptions的类型,其值是使用它们自己的类型进行定义的:target:targetRef,container:containerRef,但你没有在这个类型中定义offset。

interface UseScrollOptions {
target: TTarget,
container: type,
offset: TOffset,
}

enum TOffset { 'start end', 'end end' }

type TTarget {
enum1: type1,
enum2: type1
enum3: type1
enum4: type1
}

英文:

You must have defined a type called UseScrollOptions, which values ​​are defined with their own types: target: targetRef, container: containerRef, but you did not define the offset in this type.

interface UseScrollOptions {
target: TTarget,
container: type,
offset: TOffset,
}

enum TOffset { 'start end', 'end end'}

type TTarget {
enum1: type1,
enum2: type1
enum3: type1
enum4: type1
}

huangapple
  • 本文由 发表于 2023年7月20日 21:59:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/76730677.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定