Skip to content

Conditional Types, implement custom error message for non matching case #33098

Closed
@abdulkareemnalband

Description

@abdulkareemnalband

Search Terms

#conditionalType , #customErrorMessage

Suggestion

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;

Use Cases

Too improve the diagnostics of conditional types.

Examples

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

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions