In conditional types never is used to remove invalid types from assignments, Error message is is reported as So and so type cannot be assigned to never , Which does not inform user what are the actual requirements for usecase.
Author of conditional type need to provide some custom error message for particular failure.
this could be provided as doc comment before never
Ex:
type Test<C> = C extends some? C :/** Should extend something*/ never;
Too improve the diagnostics of conditional types.
type NonData<D> = D extends {data:any}?/**parameter should not contain property data */ never:D;
declare function nonData<T>(param:NonData<T>):void;
const withData = {data:10 , a:'a'};
nonData(withData); // error, but error message is "parameter should not contain property data "
const withNonData = { a:'a'};
nonData(withNonData); // pass
My suggestion meets these guidelines:
Duplicate of #23689.
For now I tend to use something like:
type NonData<D> = D extends {data:any} ? [D, 'should not extend { data: any }', never] : D;
which is not fool-proof, but works well enough.
And FWIW, searching "_custom error message_" will provide #30289 in the first page of results---which is almost exactly what you are asking for.
@jack-williams thanks
I could not find previous issues,
closing this now in favor of #23689
Most helpful comment
Duplicate of #23689.
For now I tend to use something like:
which is not fool-proof, but works well enough.
And FWIW, searching "_custom error message_" will provide #30289 in the first page of results---which is almost exactly what you are asking for.