简体   繁体   English

打字稿:'{translationSentences:从不[]; }' 不可分配给类型为 'never' 的参数

[英]Typescript: '{ translatedSentences: never[]; }' is not assignable to parameter of type 'never'

I know similar questions have been asked on stackoverflow, and I have been reading through them and trying to implement their solutions.我知道在 stackoverflow 上也有人问过类似的问题,我一直在阅读它们并尝试实施他们的解决方案。 But, after over an hour of trying to change a million different things I am no closer to understanding this problem or how to solve it.但是,在尝试改变一百万种不同的事情一个多小时后,我并没有更接近于理解这个问题或如何解决它。

I am trying to add types to my reducer and I'm getting nowhere with this error:我正在尝试向我的减速器添加类型,但此错误无济于事:

在此处输入图片说明

InitialState初始状态

export const initialState = {
  activeStep: 0,
  notification: {
    format: undefined,
    message: "",
  },
  lesson: {
    title: "",
    language: { language: "", code: "" },
    topics: [],
    format: "",
    url: "",
    file: EMPTY_FILE,
    totalPoints: 0,
    totalWords: 0,
  },
  newWords: [],
  initialTranscript: [],
  modifiedTranscript: [],
  transcriptSentences: [],
  translatedSentences: [],
};

Reducer减速器

export const reducer = (state: InitialState = initialState, action: Action) => {
  switch (action.type) {
    ...

    case ActionTypes.SET_SENTENCE_TRANSLATIONS:
      return {
        ...state,
        translatedSentences: action.payload,
      };
    case ActionTypes.SET_SENTENCE_TRANSLATION:
      const { sentence, id } = action.payload;
      const translatedSentencesClone = state.translatedSentences.slice();
      translatedSentencesClone[id] = sentence;
      return {
        ...state,
        translatedSentences: translatedSentencesClone,
      };

    ...

  }
}

Types类型

export type TranslatedSentence = {
  id: number;
  source: string;
  translation: string;
  approved?: boolean;
};


// for the initial batch of all translations
interface SET_SENTENCE_TRANSLATIONS {
  type: ActionTypes.SET_SENTENCE_TRANSLATIONS;
  payload: TranslatedSentence[];
}

// for updating one translations
interface SET_SENTENCE_TRANSLATION {
  type: ActionTypes.SET_SENTENCE_TRANSLATION;
  payload: { sentence: TranslatedSentence; id: number };
}

Hopefully thats enough material, if not let me know.希望这是足够的材料,如果没有让我知道。 I'm stumped and feel pretty lost at this point.在这一点上,我很难过,感觉很失落。

I was going to leave another comment, but the TS Playground link is too long to include.我打算再发表评论,但TS Playground 链接太长,无法包含。

What you want is for your reducer to take a State and an Action and return a State of the same type as the input.你需要的是你的减速采取StateAction ,并返回一个State的相同类型的输入。

Your initialState variable should fulfill your State interface, but so should any other valid state.您的initialState变量应该满足您的State接口,但任何其他有效状态也应该如此。 You will get errors if you use typeof initialState to create the State interface because this says that the property translatedSentances can only be an empty array aka never[] .如果您使用typeof initialState创建State接口,则会出现错误,因为这表明属性translatedSentances只能是一个空数组,即never[] I haven't seen the definition of your type InitialState , but it seems odd that you're using the name InitialState for your reducer's state rather than just State , so that leads me to be concerned that the contents of the InitialState type might not be what you want.我还没有看到您的类型InitialState的定义,但是您使用名称InitialState作为减速器的状态而不仅仅是State似乎很奇怪,所以这让我担心InitialState类型的内容可能不是你想要什么。

In the snippet you've provided, you're only editing the translatedSentences property, so your State type can be as simple as:在您提供的代码段中,您只编辑了translatedSentences属性,因此您的State类型可以像这样简单:

interface State {
    translatedSentences: TranslatedSentence[];
}

and your Action type is the union:并且您的Action类型是联合:

type Action = SET_SENTENCE_TRANSLATION | SET_SENTENCE_TRANSLATIONS;

Your reducer should have the signature:您的减速器应具有以下签名:

const reducer = (state: State, action: Action): State

and you should not have any problems using useReducer with that -- I hope!使用useReducer应该不会有任何问题——我希望!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React TypeScript:参数不可分配给“从不”类型的参数 - React TypeScript: Argument is not assignable to parameter of type 'never' “文件”类型的参数不能分配给“从不”类型的参数。 与 TypeScript 反应 - Argument of type 'File' is not assignable to parameter of type 'never'. React with TypeScript 'string' 类型的参数不可分配给 typeScript 中的 'never' 类型的参数 - Argument of type 'string' is not assignable to parameter of type 'never' in typeScript 与 Typescript 反应:“never[]”类型的参数不可分配给“StateProperties |”类型的参数 (() => 状态属性)' - React with Typescript: Argument of type 'never[]' is not assignable to parameter of type 'StateProperties | (() => StateProperties)' Typescript,useContext(),错误:“never[]”类型的参数不可分配给类型参数 - Typescript, useContext(), error: Argument of type 'never[]' is not assignable to parameter of type 在 Typescript 中声明类型时,“never[]”类型的参数不可分配给“never”类型的参数 - Argument of type 'never[]' is not assignable to parameter of type 'never' when declaring a type in Typescript Typescript 错误:: 类型“数字”不可分配给类型“从不” - Typescript error :: Type 'number' is not assignable to type 'never' React TypeScript:类型“string []”不可分配给类型“never []” - React TypeScript: Type 'string[]' is not assignable to type 'never[]' “状态”类型的参数不能分配给“从不”类型的参数 - Argument of type 'State' is not assignable to parameter of type 'never' 在反应 typescript 中,类型“字符串”不可分配给类型“从不”并且类型“未知”不可分配给类型“从不”错误 - Type 'string' is not assignable to type 'never' and Type 'unknown' is not assignable to type 'never' error in react typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM