programing

포함된 생성자에 반환 유형 주석이 없으므로 '유형' 식에 암묵적으로 '임의' 유형이 지정됩니다.

css3 2023. 3. 1. 11:22

포함된 생성자에 반환 유형 주석이 없으므로 '유형' 식에 암묵적으로 '임의' 유형이 지정됩니다.

첫 번째 스니펫은 내가 작업하고 있는 코드이며, 아래는 에러입니다.코드에 있는 모든 "수익률 선택" 부분에서 발생합니다.다음 단계는 잘 모르겠습니다.

function* onLoadingDomainsresult() {
  const pathname = yield select(getPathname);

  interface Params {
    hastag: string;
  }

'yield' expression implicitly results in an 'any' type because its containing generator lacks a return-type annotation.  TS7057

    113 | 
    114 | function* onLoadingDomainsresult() {
  > 115 |   const pathname = yield select(getPathname);
        |                    ^
    116 | 
    117 |   interface Params {
    118 |     hastag: string;

리터럴 타입select(getPathname)그 가치와는 무관합니다.yield.select(getPathname)는 반복적인 컨텍스트에 대한 공존에 의해 산출되는 값입니다.

실행 컨텍스트에 의해 제너레이터에 주입된 값(를 통해next()콜) 에서 반환되는 타입에 따라서는yield표현.

어느 쪽이든 현재 Typescript에는 생성기 함수에 유형 주석이 없기 때문에 얻을 수 있는 메타데이터가 전혀 없습니다.

이건 레독스가인 것 같아요

일반적인 제너레이터 함수 유형 주석은 다음과 같습니다.

type WhatYouYield="foo"
type WhatYouReturn="bar"
type WhatYouAccept="baz"

function* myfun(): Generator<
  WhatYouYield,
  WhatYouReturn,
  WhatYouAccept
> {
const myYield = "foo" //type of myYield is WhatYouYield
const myAccepted = yield myYield; //type of myAccepted is WhatYouAccept
return "baz" //type of this value is WhatYouReturn 
}

...그리고 당신이 받는 오류는 Typescript에서 나온 것입니다.WhatYouAccept함수에 제너레이터 유형 주석을 지정하지 않고 입력합니다.

똑같은 오류가 나와서 해결했어요.

export interface ResponseGenerator{
    config?:any,
    data?:any,
    headers?:any,
    request?:any,
    status?:number,
    statusText?:string
}
const response:ResponseGenerator = yield YOUR_YIELD_FUNCTION
console.log(response.data)

최근 타이프스크립트 업데이트에서는 제너레이터 함수에 대한 더 많은 타입 제한이 있습니다.

유형 1: 콜 시 양보

function* initDashboard(): any {
  let response = yield call(getDashboardData);
  console.log(response);
}

유형 2: 콜 없는 항복

function* initDashboard() {
  let response: any = yield getDashboardData;
  console.log(response);
}

주의: 사용방법any가장 빠른 해결책이지만 적절한 해결책은 응답 유형/인터페이스를 생성하여 유형으로 사용하는 것입니다.

반환 유형으로 any를 추가할 수 있습니다.

function* onLoadingDomainsresult():any {
  const pathname = yield select(getPathname);

  interface Params {
    hastag: string;
  }

언급URL : https://stackoverflow.com/questions/66922379/yield-expression-implicitly-results-in-an-any-type-because-its-containing-ge