Definitelytyped: رد فعل جهاز التوجيه: فشل "withRouter" مع بدء "ComponentType" في 3.6.2

تم إنشاؤها على ١٠ سبتمبر ٢٠١٩  ·  10تعليقات  ·  مصدر: DefinitelyTyped/DefinitelyTyped

نجح هذا في 3.5.2 ، لكنه فشل في 3.6.2.

strict معطل.

هل هذا خطأ في الكتابة أو TypeScript؟

لقد أضفت اختبارات فاشلة لهذا هنا: https://github.com/DefinitelyTyped/DefinitelyTyped/commit/f036856dfc9a7fd140f378402c102206738ec0a4.

import * as React from 'react';

// Copied from https://github.com/DefinitelyTyped/DefinitelyTyped/blob/5db94ca7fd3570dc23588b404d7bb669a7886a8a/types/react-router/index.d.ts#L146
declare function withRouter<P, C extends React.ComponentType<P>>(
    component: C & React.ComponentType<P>,
): unknown;

declare const Component: React.ComponentType<{}>;
/*
Argument of type 'ComponentType<{}>' is not assignable to parameter of type 'ComponentClass<{}, any> | (ComponentClass<{}, any> & FunctionComponent<{}>)'.
  Type 'FunctionComponent<{}>' is not assignable to type 'ComponentClass<{}, any> | (ComponentClass<{}, any> & FunctionComponent<{}>)'.
    Type 'FunctionComponent<{}>' is not assignable to type 'ComponentClass<{}, any> & FunctionComponent<{}>'.
      Type 'FunctionComponent<{}>' is not assignable to type 'ComponentClass<{}, any>'.
        Type 'FunctionComponent<{}>' provides no match for the signature 'new (props: {}, context?: any): Component<{}, any, any>'.
*/
withRouter(Component);

إذا كنت تعرف كيفية إصلاح المشكلة ، فقم بتقديم طلب سحب بدلاً من ذلك.

  • [x] حاولت استخدام الحزمة @types/xxxx وواجهت مشاكل.
  • [x] حاولت استخدام أحدث إصدار ثابت من tsc. https://www.npmjs.com/package/typescript
  • [x] لدي سؤال غير مناسب لـ StackOverflow . (يرجى طرح أي أسئلة مناسبة هناك).
  • [x] [أذكر] (https://github.com/blog/821-mention-somebody-they-re-notified) المؤلفون (انظر Definitions by: في index.d.ts ) حتى يتمكنوا من رد.

    • المؤلف: @ سيرغي-buturlakin @ mrk21 @ vasek17ngbrownawendlandKostyaEsmukovjohnnyreillyLKayDovydasNavickas @ هوي-نغوينgrmiadeDaIgebegorshulgarraina @ PRET واحد في العتال @ t49tran @ 8enSmithwezleytsai @ eps1lonHipsterBrown

إذا لم تذكر المؤلفين ، فسيتم تجاهل المشكلة.

Bug

التعليق الأكثر فائدة

ما زلت أرى مشكلات حتى بعد هذا الإصلاح. رسالة الخطأ شيء من هذا القبيل

 Argument of type '({ myProp, history }: RouteComponentProps<{}, StaticContext, any> & { myProp: boolean; }) => Element' is not assignable to parameter of type 'ComponentType<RouteComponentProps<{}, StaticContext, any>>'.
  Type '({ myProp, history }: RouteComponentProps<{}, StaticContext, any> & { myProp: boolean; }) => Element' is not assignable to type 'FunctionComponent<RouteComponentProps<{}, StaticContext, any>>'.
    Types of parameters '__0' and 'props' are incompatible.
      Type 'PropsWithChildren<RouteComponentProps<{}, StaticContext, any>>' is not assignable to type 'RouteComponentProps<{}, StaticContext, any> & { myProp: boolean; }'.
        Property 'myProp' is missing in type 'RouteComponentProps<{}, StaticContext, any> & { children?: ReactNode; }' but required in type '{ myProp: boolean; }'.

مع المكون المحدد باسم

import { RouteComponentProps, withRouter } from "react-router-dom";

interface Props extends RouteComponentProps {
  myProp: boolean;
}

const Component = ({ myProp, match }: Props) => null

export default withRouter(Component);

أنا استخدم

typescript: 3.6.3
@types/react-router: 5.0.4

ال 10 كومينتر

لن يكون لدي وقت للبحث في هذا. أعتقد أنه يجب علينا إزالة أي استخدام لـ ComponentType واستخدام وظائف عادية مع الدعائم الصريحة فيما يتعلق بـ ref و children . ComponentType دائمًا يمثل مشكلة بعض الشيء بسبب الاتحاد. إذا لم تخلق أخطاء ، فقد أنتجت اتحادات كبيرة جدًا مع مجموعات أخرى. يجب ألا يلمس مستهلكو المكتبات الإحصائيات على أي حال ، لذا لا ينبغي أن يكون فقدانها مشكلة.

حصلت على نفس المشكلة. تم الإبلاغ بالفعل عن repo جهاز التوجيه التفاعلي (https://github.com/ReactTraining/react-router/issues/6906) ولكن تمت إعادة توجيهه إلى هنا.

ما زلت أرى مشكلات حتى بعد هذا الإصلاح. رسالة الخطأ شيء من هذا القبيل

 Argument of type '({ myProp, history }: RouteComponentProps<{}, StaticContext, any> & { myProp: boolean; }) => Element' is not assignable to parameter of type 'ComponentType<RouteComponentProps<{}, StaticContext, any>>'.
  Type '({ myProp, history }: RouteComponentProps<{}, StaticContext, any> & { myProp: boolean; }) => Element' is not assignable to type 'FunctionComponent<RouteComponentProps<{}, StaticContext, any>>'.
    Types of parameters '__0' and 'props' are incompatible.
      Type 'PropsWithChildren<RouteComponentProps<{}, StaticContext, any>>' is not assignable to type 'RouteComponentProps<{}, StaticContext, any> & { myProp: boolean; }'.
        Property 'myProp' is missing in type 'RouteComponentProps<{}, StaticContext, any> & { children?: ReactNode; }' but required in type '{ myProp: boolean; }'.

مع المكون المحدد باسم

import { RouteComponentProps, withRouter } from "react-router-dom";

interface Props extends RouteComponentProps {
  myProp: boolean;
}

const Component = ({ myProp, match }: Props) => null

export default withRouter(Component);

أنا استخدم

typescript: 3.6.3
@types/react-router: 5.0.4

@ karol-majewski أنا في TS 3.6.3 والإصدار الأخير @types/react-router: 5.0.4 ينكسر ، بينما العودة إلى السابق أمر جيد.

export function withRouter<P extends RouteComponentProps<any>, C extends React.ComponentType<P>>(
  component: C & React.ComponentType<P>,
): React.ComponentClass<Omit<P, keyof RouteComponentProps<any>> & WithRouterProps<C>> & WithRouterStatics<C>;

أنا أشجع الجميع على فتح قضايا جديدة. إذا قمنا بإصلاح مشكلتك الخاصة ، فلن يكون لديك طريقة لإعلامك إذا قمت بالتعليق فقط في سلسلة محادثات مغلقة. تتيح ساحة اللعب المطبوعة الآن تكوين ts version + config وهي قادرة على تنزيل تعريفات الأنواع. يساعد تضمين ارتباط في إصدار جديد المشرفين كثيرًا. شكرا: صلوا:

يبدو أن هذا لا يحدث إلا مع strictFunctionTypes ولدي حل في الاعتبار. سنرى ما إذا كان يعمل.

sandersn هل يمكننا إعادة فتح هذا منذ أن تم إرجاع الإصلاح ؟

اكتشف IIRC ، @ eps1lon أن هذا يرجع إلى خطأ في TypeScript: https://github.com/microsoft/TypeScript/issues/33490

سيأتي الإصلاح: https://github.com/microsoft/TypeScript/pull/34607.

يعمل بشكل جيد مع الإصدار الليلي. رابط الملعب

الكتابة المطبوعة هي مضيعة للوقت ، لقد أمضيت ساعات في محاولة حل هذا الخطأ الغبي وغير ذلك ، لذلك إذا تم إنشاء النص المطبوع عليه لتسهيل الحياة ، فهو بالتأكيد لا يقوم بالمهمة ، مليء بالأخطاء والأخطاء

تم إصلاح هذه المشكلة في TS 3.7. إغلاق.

هل كانت هذه الصفحة مفيدة؟
0 / 5 - 0 التقييمات

القضايا ذات الصلة

lilling picture lilling  ·  3تعليقات

fasatrix picture fasatrix  ·  3تعليقات

jbreckmckye picture jbreckmckye  ·  3تعليقات

Loghorn picture Loghorn  ·  3تعليقات

tyv picture tyv  ·  3تعليقات