Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

what is the equal sign means in 'T extends Node = Node' #55

Closed
YeYongFen opened this issue Dec 17, 2017 · 1 comment
Closed

what is the equal sign means in 'T extends Node = Node' #55

YeYongFen opened this issue Dec 17, 2017 · 1 comment
Labels

Comments

@YeYongFen
Copy link

excuse me sir ,I am a rookie,when I red your code as follows. I have no idea what is 'T extends Node = Node' . I Look it up in the document of typescript about generic, but the question about remain unanswered,Can you give me some idea to inspire me

export interface VNode<T extends Node = Node, Props = VNodeProps<Element>> {
  tagName: string | void
  props: Props
  children: Array<VNode> | ReadonlyArray<VNode> | void
  text: string | void
  key: string | number | void
  element: T | void
  namespace: string | void
  scope: string | void

  parent: VNode<Element> | void
}
@TylorS
Copy link
Collaborator

TylorS commented Dec 17, 2017

Hello @yyf1994gggg! In TypeScript, I believe they are called "type parameter defaults" - see: microsoft/TypeScript#2175

I'll try to explain how this works in practice

// This interface *requires* a type parameter
interface Foo<A> {
  foo: A
}

// This interface provides a *default* when no type parameter is used
interface Bar<A = string> {
  bar: A
}

// always needed
const foo: Foo<number> = {  foo: 1 }
// will error about missing type parameter
const foo: Foo = { foo: 1 }

// When you want to specify something that's not the default
const nonDefaultBar: Bar<number> = { bar: 2 }
// use the default
const defaultBar: Bar = { bar: 'defaults to string' }
// This will error becase Bar<number> is not assignable to Bar
const errorBar: Bar = { bar: 2 } 

I hope this helps you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants