Skip to content

Commit 12c600f

Browse files
committed
Handle type variables more reliably
1 parent f96898b commit 12c600f

11 files changed

+485
-100
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
interface Foobar {
2+
foobar: number
3+
}
4+
5+
export interface Option<A> {
6+
value?: A
7+
}
8+
9+
export function hasArrowFunctionParameter(
10+
something: number,
11+
fn: (foobar: Foobar) => Foobar
12+
): Foobar {
13+
return fn({ foobar: 42 + something })
14+
}
15+
export function consumesArrowFunction(): number {
16+
return (
17+
hasArrowFunctionParameter(1, ({ foobar }) => ({ foobar: foobar + 1 }))
18+
.foobar +
19+
hasArrowFunctionParameter(2, foobar => ({ foobar: foobar.foobar + 2 }))
20+
.foobar
21+
)
22+
}
23+
export function genericArrow(): Foobar[] {
24+
return [1].map<Foobar>(n => ({ foobar: n + 1 }))
25+
}
26+
export function genericArrowOption(): Option<Foobar>[] {
27+
return [1].map<Option<Foobar>>(n => ({ value: { foobar: n + 1 } }))
28+
}
29+
export function genericArrow2(): Foobar[] {
30+
return [1].map(n => ({ foobar: n + 1 }))
31+
}

snapshots/input/syntax/src/object-literals-call-signatures.ts

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export function consumesInterface(superInterface: Superinterface): void {}
44
export function consumesGenericInterface<T>(
55
genercInterface: GenericInterface<T>
66
): void {}
7+
78
export function infersInterface(): void {
89
consumesInterface({
910
interfaceMethod: (): string => 'inferred',

snapshots/input/syntax/src/object-literals-fat-arrow.ts

-10
This file was deleted.

snapshots/input/syntax/src/object-literals-nested.ts

+18
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Option } from './object-literals-arrow-function'
2+
13
interface Address {
24
street: string
35
people: Person[]
@@ -20,3 +22,19 @@ export function handleNestedObjectLiterals(): Person {
2022
},
2123
}
2224
}
25+
26+
export function handleNestedTypeVariables(): Option<Person> {
27+
return {
28+
value: {
29+
name: 'John',
30+
address: {
31+
street: 'Oxford Street',
32+
people: [
33+
{
34+
name: 'Susan',
35+
},
36+
],
37+
},
38+
},
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
interface Foobar {
2+
// definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/
3+
//documentation ```ts\nmodule "object-literals-arrow-function.ts"\n```
4+
// ^^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#
5+
// documentation ```ts\ninterface Foobar\n```
6+
foobar: number
7+
// ^^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#foobar.
8+
// documentation ```ts\n(property) foobar: number\n```
9+
}
10+
11+
export interface Option<A> {
12+
// ^^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/Option#
13+
// documentation ```ts\ninterface Option\n```
14+
// ^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/Option#[A]
15+
// documentation ```ts\nA: A\n```
16+
value?: A
17+
// ^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/Option#value.
18+
// documentation ```ts\n(property) value: A\n```
19+
// ^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Option#[A]
20+
}
21+
22+
export function hasArrowFunctionParameter(
23+
// ^^^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/hasArrowFunctionParameter().
24+
// documentation ```ts\nfunction hasArrowFunctionParameter(something: number, fn: (foobar: Foobar) => Foobar): Foobar\n```
25+
something: number,
26+
// ^^^^^^^^^ definition local 0
27+
// documentation ```ts\n(parameter) something: number\n```
28+
fn: (foobar: Foobar) => Foobar
29+
// ^^ definition local 1
30+
// documentation ```ts\n(parameter) fn: (foobar: Foobar) => Foobar\n```
31+
// ^^^^^^ definition local 2
32+
// documentation ```ts\n(parameter) foobar: Foobar\n```
33+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#
34+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#
35+
): Foobar {
36+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#
37+
return fn({ foobar: 42 + something })
38+
// ^^ reference local 1
39+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#foobar.
40+
// ^^^^^^^^^ reference local 0
41+
}
42+
export function consumesArrowFunction(): number {
43+
// ^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/consumesArrowFunction().
44+
// documentation ```ts\nfunction consumesArrowFunction(): number\n```
45+
return (
46+
hasArrowFunctionParameter(1, ({ foobar }) => ({ foobar: foobar + 1 }))
47+
// ^^^^^^^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/hasArrowFunctionParameter().
48+
// ^^^^^^ definition local 5
49+
// documentation ```ts\n(parameter) foobar: number\n```
50+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#foobar.
51+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#foobar.
52+
// ^^^^^^ reference local 5
53+
.foobar +
54+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#foobar.
55+
hasArrowFunctionParameter(2, foobar => ({ foobar: foobar.foobar + 2 }))
56+
// ^^^^^^^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/hasArrowFunctionParameter().
57+
// ^^^^^^ definition local 6
58+
// documentation ```ts\n(parameter) foobar: Foobar\n```
59+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#foobar.
60+
// ^^^^^^ reference local 6
61+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#foobar.
62+
.foobar
63+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#foobar.
64+
)
65+
}
66+
export function genericArrow(): Foobar[] {
67+
// ^^^^^^^^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/genericArrow().
68+
// documentation ```ts\nfunction genericArrow(): Foobar[]\n```
69+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#
70+
return [1].map<Foobar>(n => ({ foobar: n + 1 }))
71+
// ^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#map().
72+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#
73+
// ^ definition local 7
74+
// documentation ```ts\n(parameter) n: number\n```
75+
// ^^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/foobar0:
76+
// documentation ```ts\n(property) foobar: number\n```
77+
// ^ reference local 7
78+
}
79+
export function genericArrowOption(): Option<Foobar>[] {
80+
// ^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/genericArrowOption().
81+
// documentation ```ts\nfunction genericArrowOption(): Option<Foobar>[]\n```
82+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Option#
83+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#
84+
return [1].map<Option<Foobar>>(n => ({ value: { foobar: n + 1 } }))
85+
// ^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#map().
86+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Option#
87+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#
88+
// ^ definition local 8
89+
// documentation ```ts\n(parameter) n: number\n```
90+
// ^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/value0:
91+
// documentation ```ts\n(property) value: { foobar: number; }\n```
92+
// ^^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/foobar1:
93+
// documentation ```ts\n(property) foobar: number\n```
94+
// ^ reference local 8
95+
}
96+
export function genericArrow2(): Foobar[] {
97+
// ^^^^^^^^^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/genericArrow2().
98+
// documentation ```ts\nfunction genericArrow2(): Foobar[]\n```
99+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Foobar#
100+
return [1].map(n => ({ foobar: n + 1 }))
101+
// ^^^ reference typescript 4.8.4 lib/`lib.es5.d.ts`/Array#map().
102+
// ^ definition local 9
103+
// documentation ```ts\n(parameter) n: number\n```
104+
// ^^^^^^ definition syntax 1.0.0 src/`object-literals-arrow-function.ts`/foobar2:
105+
// documentation ```ts\n(property) foobar: number\n```
106+
// ^ reference local 9
107+
}
108+

snapshots/output/syntax/src/object-literals-call-signatures.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
// ^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/GenericInterface#
2323
// ^ reference syntax 1.0.0 src/`object-literals-call-signatures.ts`/consumesGenericInterface().[T]
2424
): void {}
25+
2526
export function infersInterface(): void {
2627
// ^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`object-literals-call-signatures.ts`/infersInterface().
2728
// documentation ```ts\nfunction infersInterface(): void\n```
@@ -35,16 +36,20 @@
3536
consumesGenericInterface({
3637
// ^^^^^^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`object-literals-call-signatures.ts`/consumesGenericInterface().
3738
interfaceMethod: (): string => 'inferred',
38-
// ^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/GenericInterface#interfaceMethod().
39+
// ^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`object-literals-call-signatures.ts`/interfaceMethod0:
40+
// documentation ```ts\n(property) interfaceMethod: () => string\n```
3941
property: 123,
40-
// ^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/GenericInterface#property.
42+
// ^^^^^^^^ definition syntax 1.0.0 src/`object-literals-call-signatures.ts`/property0:
43+
// documentation ```ts\n(property) property: number\n```
4144
})
4245
consumesGenericInterface<string[]>({
4346
// ^^^^^^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`object-literals-call-signatures.ts`/consumesGenericInterface().
4447
interfaceMethod: (): string => 'inferred',
45-
// ^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/GenericInterface#interfaceMethod().
48+
// ^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`object-literals-call-signatures.ts`/interfaceMethod1:
49+
// documentation ```ts\n(property) interfaceMethod: () => string\n```
4650
property: ['goofy', 'interface'],
47-
// ^^^^^^^^ reference syntax 1.0.0 src/`inheritance.ts`/GenericInterface#property.
51+
// ^^^^^^^^ definition syntax 1.0.0 src/`object-literals-call-signatures.ts`/property1:
52+
// documentation ```ts\n(property) property: string[]\n```
4853
})
4954
}
5055

snapshots/output/syntax/src/object-literals-fat-arrow.ts

-36
This file was deleted.

snapshots/output/syntax/src/object-literals-nested.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
interface Address {
1+
import { Option } from './object-literals-arrow-function'
22
// definition syntax 1.0.0 src/`object-literals-nested.ts`/
33
//documentation ```ts\nmodule "object-literals-nested.ts"\n```
4+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Option#
5+
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/
6+
7+
interface Address {
48
// ^^^^^^^ definition syntax 1.0.0 src/`object-literals-nested.ts`/Address#
59
// documentation ```ts\ninterface Address\n```
610
street: string
@@ -45,3 +49,29 @@
4549
}
4650
}
4751

52+
export function handleNestedTypeVariables(): Option<Person> {
53+
// ^^^^^^^^^^^^^^^^^^^^^^^^^ definition syntax 1.0.0 src/`object-literals-nested.ts`/handleNestedTypeVariables().
54+
// documentation ```ts\nfunction handleNestedTypeVariables(): Option<Person>\n```
55+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Option#
56+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-nested.ts`/Person#
57+
return {
58+
value: {
59+
// ^^^^^ reference syntax 1.0.0 src/`object-literals-arrow-function.ts`/Option#value.
60+
name: 'John',
61+
// ^^^^ reference syntax 1.0.0 src/`object-literals-nested.ts`/Person#name.
62+
address: {
63+
// ^^^^^^^ reference syntax 1.0.0 src/`object-literals-nested.ts`/Person#address.
64+
street: 'Oxford Street',
65+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-nested.ts`/Address#street.
66+
people: [
67+
// ^^^^^^ reference syntax 1.0.0 src/`object-literals-nested.ts`/Address#people.
68+
{
69+
name: 'Susan',
70+
// ^^^^ reference syntax 1.0.0 src/`object-literals-nested.ts`/Person#name.
71+
},
72+
],
73+
},
74+
},
75+
}
76+
}
77+

0 commit comments

Comments
 (0)