小编yud*_*esh的帖子

打字稿错误:类型...的“this”上下文无法分配给方法的类型“this”

我目前正在调试一些库的代码,并且我一生都不理解特定的错误。

这是我正在调试的代码的简化示例:

type Test1Type<V> = object | ((this: V) => object);

function testFnc(x: string, test1Fnc: Test1Type<string>) {
  let res;

  if (typeof test1Fnc === "function") {
    res = test1Fnc.call(x, x);
  }
}
Run Code Online (Sandbox Code Playgroud)

在该行中,res = test1Fnc.call(x, x);该术语test1Fnc被标记为错误:

(parameter) test1Fnc: Function | ((this: string) => object)

The 'this' context of type 'Function | ((this: string) => object)' is not assignable to method's 'this' of type '((this: string) => object) & Function'.
  Type 'Function' is not assignable to type '((this: …
Run Code Online (Sandbox Code Playgroud)

typescript

11
推荐指数
1
解决办法
8269
查看次数

rust const fn 到底是什么意思

Rust 常量 fn

在下面的文章中,我看到了以下内容:

const fn 允许您在“const 上下文”中执行代码。例如:

const fn five() -> i32 {
    5
}

const FIVE: i32 = five();
Run Code Online (Sandbox Code Playgroud)

const fn 在 Rust 中到底做了什么?难道只是说函数可以在编译时完全计算出来(我的猜测是基于阅读文章)

constants rust

11
推荐指数
1
解决办法
3519
查看次数

使用 tf.keras.utils.Sequence 和 model.fit_generator with use_multiprocessing=True 生成警告

这是我收到的警告:

WARNING:tensorflow:multiprocessing can interact badly with TensorFlow, causing nondeterministic deadlocks. For high performance data pipelines tf.data is recommended.
Run Code Online (Sandbox Code Playgroud)

我写的 Sequence 子类严格执行加载和读取 I/O jpg 文件。我想只要没有 2 个线程在同一个文件上同时执行,事情应该没问题。

我训练了几个 epoch,到目前为止,没有错误。但是如果有可能发生的潜在不好的事情,我想获得反馈。

python multiprocessing tensorflow tf.keras

6
推荐指数
1
解决办法
4777
查看次数

找不到模块“react-split”的声明文件

我正在尝试制作拆分视图,但在导入react-split 时遇到问题。

当我导入并将其添加到我的代码中时,我没有看到任何问题。然后,我看到在我的编辑器中,import Split from "react-split"有三个点,当我将它们悬停时,它说:

找不到模块“react-split”的声明文件。'/home/user/Documents/test/node_modules/react-split/dist/react-split.js' 隐式有一个 'any' 类型。尝试npm install @types/react-split它是否存在或添加一个包含declare module 'react-split';ts(7016)的新声明 (.d.ts) 文件

如果我尝试运行 npm install @types/react-split 我得到

npm 错误!代码 E404 npm ERR!404 未找到 - GET https://registry.npmjs.org/@types%2freact-split - 未找到 npm ERR!404 npm 错误!404 '@types/react-split@latest' 不在 npm 注册表中。npm 错误!404 你应该bug 作者发布它(或者你自己使用这个名字!) npm ERR!404 npm 错误!404 请注意,您也可以从 npm ERR 安装!404 tarball、文件夹、http url 或 git url。

当我创建一个 .d.ts 文件并声明模块时,我仍然没有解决这个问题,消息仍然存在。

知道如何解决这个问题吗?我尝试了建议的所有内容,并看到了一些类似的堆栈问题,但没有任何解决方案。

编辑:我对网络开发很陌生,它似乎要求打字稿?我想知道为什么?我安装了打字稿,但该软件包仍然无法正常工作

npm reactjs

5
推荐指数
1
解决办法
345
查看次数

在 Typescript 中使用自定义键展平对象

我想扁平化对象并将返回值转换为类型。

例如:

const myObject = {
  names: {
    title: 'red',
    subtitle: 'green'
  },
}
const returned = [...Object.values(flatten(myObject))] as const
// returns ['red', 'green']
type Type = typeof returned[number]
Run Code Online (Sandbox Code Playgroud)

现在返回的变量是 ['red', 'green']

类型应为“红色|” 'green',但现在是一个字符串数组,因为返回的 typeof 是 string[]。我想使用这种类型为我的组件输入 prop:

<Component name="red" /> //is correct, but
<Component name=`something different than "red" or "green"` /> //is incorrect.
Run Code Online (Sandbox Code Playgroud)

压平函数:

type FlattenedObject = { [x: string]: string }

export const flattenDaisyChain = (obj: any): FlattenedObject => {
  const result: FlattenedObject = {}

  const transform = …
Run Code Online (Sandbox Code Playgroud)

flatten typescript

5
推荐指数
1
解决办法
3051
查看次数

Vim彻底坏了,无法使用

在我的终端中打开 vim 时,我收到此错误

Error detected while processing CursorMoved Autocommands for "*"..function <SNR>
6_Highlight_Matching_Pair:
Run Code Online (Sandbox Code Playgroud)

它在启动 vim 时重复多次,然后我可以访问 vim,但是任何移动(lkjh)都会导致错误发生更多。

我已经删除了我的.vimrc,通过brew更新了我的vim。两者都没有任何影响,vim 是我首选的 IDE,因为我用插件以​​我更喜欢的编程方式高效地设置了它。

有人可以帮我弄清楚发生了什么吗?另外,可能重要的信息是我最近运行了一次brew更新来更新我的包管理器中的所有内容。

macos vim vim-plugin

4
推荐指数
1
解决办法
1167
查看次数

类型“CognitoUserSession”上不存在属性“idToken”

payload我正在尝试从idToken内部解构userData

userData属于 类型CognitoUserSession

import { CognitoUser } from "@aws-amplify/auth";
import { CognitoUserSession, CognitoIdToken } from "amazon-cognito-identity-js";

  const userData = await Auth.currentSession().catch(e => console.log(e));
  if (!userData) {
    console.log(`User data : ${userData}`);
    updateUser({} as CognitoUser);
    return;
  }
    const {
    idToken: { payload: CognitoIdToken }
  } : CognitoUserSession = userData;
Run Code Online (Sandbox Code Playgroud)

但我得到的错误idToken does not exist on type CognitoUserSession是它存在于它的类中。

export interface ICognitoUserSessionData {
    IdToken: CognitoIdToken;
    AccessToken: CognitoAccessToken;
    RefreshToken?: CognitoRefreshToken;
}

export class CognitoUserSession {
    constructor(data: …
Run Code Online (Sandbox Code Playgroud)

typescript aws-amplify

3
推荐指数
1
解决办法
2432
查看次数

打字稿:有递归keyof吗?

有没有办法让这样的代码编译并保持类型安全?

type ComplexObject = {
  primitive1: boolean;
  complex: {
    primitive2: string;
    primitive3: boolean;
  }
};

interface MyReference {
  myKey: keyof ComplexObject;
}

const works1: MyReference = {
  myKey: "primitive1"
}

const works2: MyReference = {
  myKey: "complex"
}

const iWantThisToCompile1: MyReference = {
  myKey: "complex.primitive2" // Error: Type '"complex.primitive2"' is not assignable to type '"primitive1" | "complex"'.
}

const iWantThisToCompile2: MyReference = {
  myKey: "complex['primitive3']" // Error: Type '"complex['primitive3']"' is not assignable to type '"primitive1" | "complex"'.
}

// const …
Run Code Online (Sandbox Code Playgroud)

types typescript

3
推荐指数
2
解决办法
805
查看次数

React Hooks 和 React Query (useQuery):仅在单击按钮时调用 API 调用/查询

我正在开发一个 React JS 项目。我正在使用 React Hooks (功能组件)和React query

我通常在功能组件中运行/执行查询,如下所示。

const ItemList = (props) => {
  let getItemsQuery = useQuery([ 'get-items' ], getItems, {
    retry: false,
    refetchOnWindowFocus: false
  });

  // The rest of the code goes here
}
Run Code Online (Sandbox Code Playgroud)

基本上,查询是在组件加载时执行的,因此 API 调用也会进行。

现在,我正在寻找一种只能在单击按钮时执行查询的方法。

return (
   <button onClick={() => {
       //make the call. The value or value might be overridden when the API call is made when the Call 2 button is clicked
    }}>Call 1</button>
   <button onClick={() => {
       //make the …
Run Code Online (Sandbox Code Playgroud)

reactjs react-hooks react-query

2
推荐指数
1
解决办法
9538
查看次数

使用 Pandas 从另一列中查找满足条件的值范围

这是数据框,其中每列的前缀为cs

c表示一个班级(是或否)并s表示与该班级相关的分数。

     cAGR cCON cEXT cNEU cOPN  sAGR  sCON  sEXT  sNEU  sOPN
2157    y    y    y    n    y  4.17  3.67  4.33  2.00  4.40
2422    y    n    y    n    y  3.95  3.25  4.20  2.60  5.00
2741    y    n    n    n    y  4.00  3.00  2.75  2.50  4.75
2884    y    y    y    n    y  3.55  3.95  3.75  2.05  3.80
4830    n    n    n    y    y  3.05  3.05  3.40  2.80  4.35
4932    y    n    n    y    y …
Run Code Online (Sandbox Code Playgroud)

python pandas

2
推荐指数
1
解决办法
41
查看次数