Skip to content

Assignment doesn't have transitivity.Β #42479

@heroboy

Description

@heroboy

Suggestion

πŸ” Search Terms

extends conditional constrain

βœ… Viability 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, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

short example:

declare function SampleComponent(props: { a?: any; }, context?: any): React.ReactElement<any, any> | null;
declare var Tag1: React.FunctionComponent<{ a?: any; }>;
declare var Tag2: React.FunctionComponent<{}>;
declare var Tag3: typeof SampleComponent;

//Tag1 and Tag3 can assign to each other.
Tag1 = Tag3;
Tag3 = Tag1;

//Tag1 and Tag2 can assign to each other.
Tag1 = Tag2;
Tag2 = Tag1;

//But Tag2 and Tag3 can not assign to each other.
Tag2 = Tag3; //error
Tag3 = Tag2; //error

playground

πŸ“ƒ Motivating Example

I want to write a function to take any function component that can accept empty props.

function myrender<T extends {}>(Tag: React.FunctionComponent<T>) {
    return <Tag /> //error
}

function myrender2(Tag:React.FunctionComponent<{}>)
{
    return <Tag/>
}
myrender2(Tag3); //error

πŸ’» Use Cases

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