-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Optional class properties #8625
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
7706f38
Minor cleanup of getFlowTypeOfReference parameters
ahejlsberg b90761c
Allow class properties and methods to be declared optional using '?'
ahejlsberg 20e2be2
Properly display optional methods in language service
ahejlsberg b8d2f2d
Accepting new baselines
ahejlsberg 6b3fc7f
Remove nullability from function types in language service
ahejlsberg 3662c7b
Adding test
ahejlsberg d66377d
Add optionality to properties declared with '?' and initializer
ahejlsberg a11f72f
Emit '?' for optional parameter property in declaration file
ahejlsberg 0292eaa
Accepting new baselines
ahejlsberg e82bbce
Fixing test
ahejlsberg 8498ef1
Adding more tests
ahejlsberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 0 additions & 26 deletions
26
tests/baselines/reference/classWithOptionalParameter.errors.txt
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
tests/baselines/reference/classWithOptionalParameter.symbols
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
=== tests/cases/conformance/types/namedTypes/classWithOptionalParameter.ts === | ||
// classes do not permit optional parameters, these are errors | ||
|
||
class C { | ||
>C : Symbol(C, Decl(classWithOptionalParameter.ts, 0, 0)) | ||
|
||
x?: string; | ||
>x : Symbol(C.x, Decl(classWithOptionalParameter.ts, 2, 9)) | ||
|
||
f?() {} | ||
>f : Symbol(C.f, Decl(classWithOptionalParameter.ts, 3, 15)) | ||
} | ||
|
||
class C2<T> { | ||
>C2 : Symbol(C2, Decl(classWithOptionalParameter.ts, 5, 1)) | ||
>T : Symbol(T, Decl(classWithOptionalParameter.ts, 7, 9)) | ||
|
||
x?: T; | ||
>x : Symbol(C2.x, Decl(classWithOptionalParameter.ts, 7, 13)) | ||
>T : Symbol(T, Decl(classWithOptionalParameter.ts, 7, 9)) | ||
|
||
f?(x: T) {} | ||
>f : Symbol(C2.f, Decl(classWithOptionalParameter.ts, 8, 10)) | ||
>x : Symbol(x, Decl(classWithOptionalParameter.ts, 9, 7)) | ||
>T : Symbol(T, Decl(classWithOptionalParameter.ts, 7, 9)) | ||
} |
26 changes: 26 additions & 0 deletions
26
tests/baselines/reference/classWithOptionalParameter.types
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
=== tests/cases/conformance/types/namedTypes/classWithOptionalParameter.ts === | ||
// classes do not permit optional parameters, these are errors | ||
|
||
class C { | ||
>C : C | ||
|
||
x?: string; | ||
>x : string | ||
|
||
f?() {} | ||
>f : () => void | ||
} | ||
|
||
class C2<T> { | ||
>C2 : C2<T> | ||
>T : T | ||
|
||
x?: T; | ||
>x : T | ||
>T : T | ||
|
||
f?(x: T) {} | ||
>f : (x: T) => void | ||
>x : T | ||
>T : T | ||
} |
4 changes: 2 additions & 2 deletions
4
tests/baselines/reference/objectLiteralMemberWithQuestionMark1.errors.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
tests/cases/compiler/objectLiteralMemberWithQuestionMark1.ts(1,14): error TS1112: A class member cannot be declared optional. | ||
tests/cases/compiler/objectLiteralMemberWithQuestionMark1.ts(1,14): error TS1162: An object member cannot be declared optional. | ||
|
||
|
||
==== tests/cases/compiler/objectLiteralMemberWithQuestionMark1.ts (1 errors) ==== | ||
var v = { foo?() { } } | ||
~ | ||
!!! error TS1112: A class member cannot be declared optional. | ||
!!! error TS1162: An object member cannot be declared optional. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//// [optionalMethods.ts] | ||
|
||
interface Foo { | ||
a: number; | ||
b?: number; | ||
f(): number; | ||
g?(): number; | ||
} | ||
|
||
function test1(x: Foo) { | ||
x.a; | ||
x.b; | ||
x.f; | ||
x.g; | ||
let f1 = x.f(); | ||
let g1 = x.g && x.g(); | ||
let g2 = x.g ? x.g() : 0; | ||
} | ||
|
||
class Bar { | ||
a: number; | ||
b?: number; | ||
f() { | ||
return 1; | ||
} | ||
g?(): number; // Body of optional method can be omitted | ||
h?() { | ||
return 2; | ||
} | ||
} | ||
|
||
function test2(x: Bar) { | ||
x.a; | ||
x.b; | ||
x.f; | ||
x.g; | ||
let f1 = x.f(); | ||
let g1 = x.g && x.g(); | ||
let g2 = x.g ? x.g() : 0; | ||
let h1 = x.h && x.h(); | ||
let h2 = x.h ? x.h() : 0; | ||
} | ||
|
||
|
||
//// [optionalMethods.js] | ||
function test1(x) { | ||
x.a; | ||
x.b; | ||
x.f; | ||
x.g; | ||
var f1 = x.f(); | ||
var g1 = x.g && x.g(); | ||
var g2 = x.g ? x.g() : 0; | ||
} | ||
var Bar = (function () { | ||
function Bar() { | ||
} | ||
Bar.prototype.f = function () { | ||
return 1; | ||
}; | ||
Bar.prototype.h = function () { | ||
return 2; | ||
}; | ||
return Bar; | ||
}()); | ||
function test2(x) { | ||
x.a; | ||
x.b; | ||
x.f; | ||
x.g; | ||
var f1 = x.f(); | ||
var g1 = x.g && x.g(); | ||
var g2 = x.g ? x.g() : 0; | ||
var h1 = x.h && x.h(); | ||
var h2 = x.h ? x.h() : 0; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
comments are out of date now.