Skip to content

Commit 4c6bf65

Browse files
committed
Partial Types (microsoft#11233)
1 parent d147616 commit 4c6bf65

File tree

7 files changed

+62
-2
lines changed

7 files changed

+62
-2
lines changed

src/compiler/checker.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4475,6 +4475,8 @@ namespace ts {
44754475
}
44764476
else if ((<ObjectType>type).objectFlags & ObjectFlags.Anonymous) {
44774477
resolveAnonymousTypeMembers(<AnonymousType>type);
4478+
} else if ((<ObjectType>type).objectFlags & ObjectFlags.Partial) {
4479+
resolvePartialTypeMembers(<PartialType>type);
44784480
}
44794481
else if ((<ObjectType>type).flags & TypeFlags.Partial) {
44804482
resolvePartialTypeMembers(<PartialType>type);
@@ -5753,6 +5755,7 @@ namespace ts {
57535755
return links.resolvedType;
57545756
}
57555757

5758+
<<<<<<< d147616ccc57b7c9f6418074c8edffd3ee258961
57565759
<<<<<<< 7b34b612beda66b0812462a3feeabc63852cd842
57575760
function getIndexTypeForTypeParameter(type: TypeParameter) {
57585761
if (!type.resolvedIndexType) {
@@ -5903,6 +5906,14 @@ namespace ts {
59035906
return links.resolvedType;
59045907
}
59055908

5909+
function getTypeFromPartialTypeNode(node: PartialTypeNode): Type {
5910+
const links = getNodeLinks(node);
5911+
if (!links.resolvedType) {
5912+
links.resolvedType = getPartialType(getTypeOfNode(node.type));
5913+
}
5914+
return links.resolvedType;
5915+
}
5916+
59065917
function getPartialType(type: Type): Type {
59075918
if (type.resolvedPartialType) {
59085919
return type.resolvedPartialType;

src/compiler/types.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -887,6 +887,7 @@ namespace ts {
887887
type: TypeNode;
888888
}
889889

890+
<<<<<<< d147616ccc57b7c9f6418074c8edffd3ee258961
890891
export interface TypeOperatorNode extends TypeNode {
891892
kind: SyntaxKind.TypeOperator;
892893
operator: SyntaxKind.KeyOfKeyword;
@@ -899,6 +900,14 @@ namespace ts {
899900
indexType: TypeNode;
900901
}
901902

903+
=======
904+
// @kind(SyntaxKind.PartialType)
905+
export interface PartialTypeNode extends TypeNode {
906+
type: TypeNode;
907+
}
908+
909+
// @kind(SyntaxKind.StringLiteralType)
910+
>>>>>>> Partial Types (#11233)
902911
export interface LiteralTypeNode extends TypeNode {
903912
kind: SyntaxKind.LiteralType;
904913
literal: Expression;
@@ -2692,8 +2701,8 @@ namespace ts {
26922701
/* @internal */
26932702
ContainsObjectLiteral = 1 << 22, // Type is or contains object literal type
26942703
/* @internal */
2695-
ContainsAnyFunctionType = 1 << 23, // Type is or contains object literal type
2696-
Partial = 1 << 24, // Partial type
2704+
ContainsAnyFunctionType = 1 << 21, // Type is or contains object literal type
2705+
Partial = 1 << 22, // Partial type
26972706

26982707
/* @internal */
26992708
Nullable = Undefined | Null,

tests/baselines/reference/partialType2.errors.txt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,43 @@
1+
<<<<<<< d147616ccc57b7c9f6418074c8edffd3ee258961
12
tests/cases/compiler/partialType2.ts(10,5): error TS2322: Type 'partial T' is not assignable to type 'T'.
3+
=======
4+
tests/cases/compiler/partialType2.ts(8,12): error TS2304: Cannot find name 'subset'.
5+
tests/cases/compiler/partialType2.ts(8,19): error TS1005: '=' expected.
6+
tests/cases/compiler/partialType2.ts(8,19): error TS2693: 'T' only refers to a type, but is being used as a value here.
7+
>>>>>>> Partial Types (#11233)
28
tests/cases/compiler/partialType2.ts(16,5): error TS2322: Type '{ name: string; length: number; foo: number; }' is not assignable to type 'partial T'.
39
tests/cases/compiler/partialType2.ts(18,11): error TS2339: Property 'name' does not exist on type 'partial T'.
410

511

12+
<<<<<<< d147616ccc57b7c9f6418074c8edffd3ee258961
613
==== tests/cases/compiler/partialType2.ts (3 errors) ====
14+
=======
15+
==== tests/cases/compiler/partialType2.ts (5 errors) ====
16+
>>>>>>> Partial Types (#11233)
717
interface State {
818
name: string;
919
length: number;
1020
foo?: number;
1121
}
1222

1323
function doSomething1<T>(x: T) {
24+
<<<<<<< d147616ccc57b7c9f6418074c8edffd3ee258961
1425
let y: partial T = <any>null;
1526
y = x; // Should be OK
1627
x = y; // Error
1728
~
1829
!!! error TS2322: Type 'partial T' is not assignable to type 'T'.
30+
=======
31+
let y: subset T = <any>null;
32+
~~~~~~
33+
!!! error TS2304: Cannot find name 'subset'.
34+
~
35+
!!! error TS1005: '=' expected.
36+
~
37+
!!! error TS2693: 'T' only refers to a type, but is being used as a value here.
38+
y = x; // Should be OK
39+
x = y; // Error
40+
>>>>>>> Partial Types (#11233)
1941
}
2042

2143
function doSomething2<T extends State>(x: T) {

tests/baselines/reference/partialType2.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ interface State {
66
}
77

88
function doSomething1<T>(x: T) {
9+
<<<<<<< d147616ccc57b7c9f6418074c8edffd3ee258961
910
let y: partial T = <any>null;
11+
=======
12+
let y: subset T = <any>null;
13+
>>>>>>> Partial Types (#11233)
1014
y = x; // Should be OK
1115
x = y; // Error
1216
}
@@ -28,7 +32,11 @@ function doSomething2<T extends State>(x: T) {
2832

2933
//// [partialType2.js]
3034
function doSomething1(x) {
35+
<<<<<<< d147616ccc57b7c9f6418074c8edffd3ee258961
3136
var y = null;
37+
=======
38+
var y = T = null;
39+
>>>>>>> Partial Types (#11233)
3240
y = x; // Should be OK
3341
x = y; // Error
3442
}

tests/baselines/reference/partialType4.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ subs.name = false; // not ok
2727
// Errors
2828
subs['err'] = false;
2929
subs[12] = false;
30+
<<<<<<< d147616ccc57b7c9f6418074c8edffd3ee258961
3031

3132

3233
//// [partialType4.d.ts]
@@ -36,3 +37,5 @@ interface State1 {
3637
[key: string]: string | number;
3738
}
3839
declare const subs: partial State1;
40+
=======
41+
>>>>>>> Partial Types (#11233)

tests/cases/compiler/partialType2.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,11 @@ interface State {
55
}
66

77
function doSomething1<T>(x: T) {
8+
<<<<<<< d147616ccc57b7c9f6418074c8edffd3ee258961
89
let y: partial T = <any>null;
10+
=======
11+
let y: subset T = <any>null;
12+
>>>>>>> Partial Types (#11233)
913
y = x; // Should be OK
1014
x = y; // Error
1115
}

tests/cases/compiler/partialType4.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
// @strictNullChecks: true
2+
<<<<<<< d147616ccc57b7c9f6418074c8edffd3ee258961
23
// @declaration: true
4+
=======
5+
>>>>>>> Partial Types (#11233)
36

47
interface State1 {
58
name: string;

0 commit comments

Comments
 (0)