Skip to content

Commit 19d0548

Browse files
author
Kanchalai Tanglertsampan
committed
Add tests for generic stateless function component
1 parent 9e3da08 commit 19d0548

4 files changed

Lines changed: 118 additions & 0 deletions
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
10+
declare function ComponentWithTwoAttributes<K,V>(l: {key1: K, value: V}): JSX.Element;
11+
12+
// OK
13+
function Baz<T,U>(key1: T, value: U) {
14+
let a0 = <ComponentWithTwoAttributes key1={key1} value={value} />
15+
let a1 = <ComponentWithTwoAttributes {...{key1, value: value}} key="Component" />
16+
}
17+
18+
// OK
19+
declare function Component<U>(l: U): JSX.Element;
20+
function createComponent<T extends {prop: number}>(arg:T) {
21+
let a1 = <Component {...arg} />;
22+
let a2 = <Component {...arg} prop1 />;
23+
}
24+
25+
declare function ComponentSpecific<U>(l: {prop: U}): JSX.Element;
26+
declare function ComponentSpecific1<U>(l: {prop: U, "ignore-prop": number}): JSX.Element;
27+
28+
// OK
29+
function Bar<T extends {prop: number}>(arg: T) {
30+
let a1 = <ComponentSpecific {...arg} ignore-prop="hi" />; // U is number
31+
let a2 = <ComponentSpecific1 {...arg} ignore-prop={10} />; // U is number
32+
let a3 = <ComponentSpecific {...arg} prop="hello" />; // U is "hello"
33+
}
34+
35+
declare function Link<U>(l: {func: (arg: U)=>void}): JSX.Element;
36+
37+
// OK
38+
function createLink(func: (a: number)=>void) {
39+
let o = <Link func={func} />
40+
}
41+
42+
function createLink1(func: (a: number)=>boolean) {
43+
let o = <Link func={func} />
44+
}
45+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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+
declare function ComponentSpecific1<U>(l: {prop: U, "ignore-prop": string}): JSX.Element;
10+
declare function ComponentSpecific2<U>(l: {prop: U}): JSX.Element;
11+
12+
// Error
13+
function Bar<T extends {prop: number}>(arg: T) {
14+
let a1 = <ComponentSpecific1 {...arg} ignore-prop={10} />;
15+
}
16+
17+
// Error
18+
function Baz<T>(arg: T) {
19+
let a0 = <ComponentSpecific1 {...arg} />
20+
}
21+
22+
declare function Link<U>(l: {func: (arg: U)=>void}): JSX.Element;
23+
24+
// Error
25+
function createLink(func: (a: number, b: string)=>void) {
26+
let o = <Link func={func} />
27+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
declare function OverloadComponent<U>(): JSX.Element;
10+
declare function OverloadComponent<U>(attr: {b: U, a?: string, "ignore-prop": boolean}): JSX.Element;
11+
declare function OverloadComponent<T, U>(attr: {b: U, a: T}): JSX.Element;
12+
13+
// OK
14+
function Baz<T extends {b: number}, U extends {a: boolean, b:string}>(arg1: T, arg2: U) {
15+
let a0 = <OverloadComponent {...arg1} a="hello" ignore-prop />;
16+
let a1 = <OverloadComponent {...arg2} ignore-pro="hello world" />;
17+
let a2 = <OverloadComponent {...arg2} />;
18+
let a3 = <OverloadComponent {...arg1} ignore-prop />;
19+
let a4 = <OverloadComponent />;
20+
let a5 = <OverloadComponent {...arg2} ignore-prop="hello" {...arg1} />;
21+
let a6 = <OverloadComponent {...arg2} ignore-prop {...arg1} />;
22+
}
23+
24+
declare function Link<U>(l: {func: (arg: U)=>void}): JSX.Element;
25+
declare function Link<U>(l: {func: (arg1:U, arg2: string)=>void}): JSX.Element;
26+
function createLink(func: (a: number)=>void) {
27+
let o = <Link func={func} />
28+
let o1 = <Link func={(a:number, b:string)=>{}} />;
29+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
declare function OverloadComponent<U>(): JSX.Element;
10+
declare function OverloadComponent<U>(attr: {b: U, a: string, "ignore-prop": boolean}): JSX.Element;
11+
declare function OverloadComponent<T, U>(attr: {b: U, a: T}): JSX.Element;
12+
13+
// Error
14+
function Baz<T extends {b: number}, U extends {a: boolean, b:string}>(arg1: T, arg2: U) {
15+
let a0 = <OverloadComponent a={arg1.b}/>
16+
let a2 = <OverloadComponent {...arg1} ignore-prop /> // missing a
17+
}

0 commit comments

Comments
 (0)