microsoft / microsoft/TypeScript
Performance issues when using generic constraints
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.4k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 117
Description
Bug Report
Using generic constraints makes IntelliSense a bit slow, but applying the same constraint on a conditional type does not.
🔎 Search Terms
constraints, generic constraints, conditional type constraints, intellisense, performance
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about constraints
⏯ Playground Link
Playground / Using generic constraints
Playground / Using conditional type constraints
💻 Code
Using generic constraints
import React from "react";
type PropsWithAs<Props, As extends React.ElementType> = Props &
React.ComponentPropsWithoutRef<As> & { as?: As };
type Render<Props, As extends React.ElementType> = (
props: PropsWithAs<Props, As>,
ref: React.ForwardedRef<React.ComponentRef<As>>
) => React.ReactElement | null;
type Component<Props, DefaultAs extends React.ElementType> = <
As extends React.ElementType = DefaultAs
>(
props: PropsWithAs<Props, As> & React.RefAttributes<React.ComponentRef<As>>
) => React.ReactElement | null;
export const forwardRef = <Props, DefaultAs extends React.ElementType>(
render: Render<Props, DefaultAs>
) => React.forwardRef(render) as Component<Props, DefaultAs>;
interface TestProps {
variant?: "a" | "b" | "c";
}
const Test = forwardRef<TestProps, "button">(() => <div />);
<Test as="a" href="/" />;
Using conditional type constraints
import React from "react";
type PropsWithAs<Props, As> = As extends React.ElementType
? Props & React.ComponentPropsWithoutRef<As> & { as?: As }
: never;
type GetComponentRef<As> = As extends React.ElementType
? React.ComponentRef<As>
: never;
type Render<Props, As> = (
props: PropsWithAs<Props, As>,
ref: React.ForwardedRef<GetComponentRef<As>>
) => React.ReactElement | null;
type Component<Props, DefaultAs> = <As = DefaultAs>(
props: PropsWithAs<Props, As> & React.RefAttributes<GetComponentRef<As>>
) => React.ReactElement | null;
export const forwardRef = <Props, DefaultAs>(
render: Render<Props, DefaultAs>
) => React.forwardRef(render) as Component<Props, DefaultAs>;
interface TestProps {
variant?: "a" | "b" | "c";
}
const Test = forwardRef<TestProps, "button">(() => <div />);
<Test as="a" href="/" />;
🙁 Actual behavior
IntelliSense is slowed down using generic constraints, but using the same contraint on a conditional type does not.
🙂 Expected behavior
Using a constraint directly on generics or on a conditional type should have no difference in IntelliSense performance.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by comparing the two linked TypeScript Playground examples, focusing on IntelliSense responsiveness with generic constraints versus conditional type constraints. Reproduce the slowdown and investigate the relevant TypeScript IntelliSense behavior; done means the two equivalent constraint forms have no meaningful performance difference.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- react, typescript
- Domain
- developer-experience, performance
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100