نجح هذا في 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);
إذا كنت تعرف كيفية إصلاح المشكلة ، فقم بتقديم طلب سحب بدلاً من ذلك.
@types/xxxx
وواجهت مشاكل.Definitions by:
في index.d.ts
) حتى يتمكنوا من رد.إذا لم تذكر المؤلفين ، فسيتم تجاهل المشكلة.
لن يكون لدي وقت للبحث في هذا. أعتقد أنه يجب علينا إزالة أي استخدام لـ 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
ولدي حل في الاعتبار. سنرى ما إذا كان يعمل.
اكتشف IIRC ، @ eps1lon أن هذا يرجع إلى خطأ في TypeScript: https://github.com/microsoft/TypeScript/issues/33490
سيأتي الإصلاح: https://github.com/microsoft/TypeScript/pull/34607.
يعمل بشكل جيد مع الإصدار الليلي. رابط الملعب
الكتابة المطبوعة هي مضيعة للوقت ، لقد أمضيت ساعات في محاولة حل هذا الخطأ الغبي وغير ذلك ، لذلك إذا تم إنشاء النص المطبوع عليه لتسهيل الحياة ، فهو بالتأكيد لا يقوم بالمهمة ، مليء بالأخطاء والأخطاء
تم إصلاح هذه المشكلة في TS 3.7. إغلاق.
التعليق الأكثر فائدة
ما زلت أرى مشكلات حتى بعد هذا الإصلاح. رسالة الخطأ شيء من هذا القبيل
مع المكون المحدد باسم
أنا استخدم