Giter Site home page Giter Site logo

Comments (2)

ttys026 avatar ttys026 commented on June 30, 2024 1

之前也有遇到过这个问题,同样的代码在不同的机器上 ts 推断的行为还不太一致。因为 TS 一直没有定位类型推导是 safe 或者是 sound 的,从不解构是可以拿到类型,解构之后就拿不到了。感觉更像是 ts 的 bug。目前推测是当数据类型足够复杂的时候,ts 会放弃自动推导类型,fallback 成 any.

一些 workarounds:

  1. 协助 ts 推断出 useModel 的返回类型,例如:
    const { title } = useModel<'useAppModel'>('useAppModel');
  2. 类型 cast:
import { useModel, Models } from 'umi';

export default function useMessageModel() {
  const { title } = useModel('useAppModel') as Models<'useAppModel'>;
}

from plugins.

leftstick avatar leftstick commented on June 30, 2024 1

之前也有遇到过这个问题,同样的代码在不同的机器上 ts 推断的行为还不太一致。因为 TS 一直没有定位类型推导是 safe 或者是 sound 的,从不解构是可以拿到类型,解构之后就拿不到了。感觉更像是 ts 的 bug。目前推测是当数据类型足够复杂的时候,ts 会放弃自动推导类型,fallback 成 any.

一些 workarounds:

  1. 协助 ts 推断出 useModel 的返回类型,例如:
    const { title } = useModel<'useAppModel'>('useAppModel');
  2. 类型 cast:
import { useModel, Models } from 'umi';

export default function useMessageModel() {
  const { title } = useModel('useAppModel') as Models<'useAppModel'>;
}

如下指定一下类型也行,就是觉得有点烦。。。

import { useModel } from 'umi';

export default function useMessageModel() {
  const { title }: { title: string } = useModel('useAppModel');
}

那就先这样吧,似乎也没什么更秒的方案了

from plugins.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.