Skip to content

Fix generic parameter defaults section in Generics.md - #2986

Merged
Jake Bailey (jakebailey) merged 1 commit into
microsoft:v2from
psmolak:patch-1
Jun 2, 2024
Merged

Fix generic parameter defaults section in Generics.md#2986
Jake Bailey (jakebailey) merged 1 commit into
microsoft:v2from
psmolak:patch-1

Conversation

@psmolak

Copy link
Copy Markdown
Contributor

Let's suppose that

class Animal {
  name: string = "TS";
}

then the first create declaration

declare function create(): Container<HTMLDivElement, HTMLDivElement[]>;
declare function create<T extends HTMLElement>(element: T): Container<T, T[]>;
declare function create<T extends HTMLElement, U extends HTMLElement>(
  element: T,
  children: U[]
): Container<T, U[]>;

correctly catches create(new HTMLElement(), [new Animal()]) as type error, whereas the shortened version, that is

declare function create<T extends HTMLElement = HTMLDivElement, U = T[]>(
  element?: T,
  children?: U
): Container<T, U>;

does allow create(new HTMLElement(), [new Animal()]).

@jakebailey Jake Bailey (jakebailey) left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally find this example odd due to the default being T[] (why would the children be the same type as the parent?) but that's preexisting and the PR is correct to my eye.

@jakebailey
Jake Bailey (jakebailey) enabled auto-merge (squash) June 2, 2024 19:21
@jakebailey
Jake Bailey (jakebailey) merged commit 24500d5 into microsoft:v2 Jun 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants