Skip to content

Commit 70ca18e

Browse files
author
Kanchalai Tanglertsampan
committed
Add a test for contextual type in JSXAttributes
1 parent 19d0548 commit 70ca18e

1 file changed

Lines changed: 42 additions & 0 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// @filename: file.tsx
2+
// @jsx: preserve
3+
// @module: amd
4+
// @noLib: true
5+
// @libFiles: react.d.ts,lib.d.ts
6+
7+
import React = require('react')
8+
9+
export interface ClickableProps {
10+
children?: string;
11+
className?: string;
12+
}
13+
14+
export interface ButtonProps extends ClickableProps {
15+
onClick: (k: "left" | "right") => void;
16+
}
17+
18+
export interface LinkProps extends ClickableProps {
19+
goTo: "home" | "contact";
20+
}
21+
22+
export function MainButton(buttonProps: ButtonProps): JSX.Element;
23+
export function MainButton(linkProps: LinkProps): JSX.Element;
24+
export function MainButton(props: ButtonProps | LinkProps): JSX.Element {
25+
const linkProps = props as LinkProps;
26+
if(linkProps.goTo) {
27+
return this._buildMainLink(props);
28+
}
29+
30+
return this._buildMainButton(props);
31+
}
32+
33+
const b0 = <MainButton {...{onClick: (k) => {console.log(k)}}} extra />; // k has type any
34+
const b2 = <MainButton onClick={(k)=>{console.log(k)}} extra />; // k has type "left" | "right"
35+
const b3 = <MainButton {...{goTo:"home"}} extra />; // goTo has type"home" | "contact"
36+
const b4 = <MainButton goTo="home" extra />; // goTo has type "home" | "contact"
37+
38+
export function NoOverload(buttonProps: ButtonProps): JSX.Element { return undefined }
39+
const c1 = <NoOverload {...{onClick: (k) => {console.log(k)}}} extra />; // k has type any
40+
41+
export function NoOverload1(linkProps: LinkProps): JSX.Element { return undefined }
42+
const d1 = <NoOverload1 {...{goTo:"home"}} extra />; // goTo has type "home" | "contact"

0 commit comments

Comments
 (0)