Skip to content

Commit 6a164ec

Browse files
committed
fix as review suggest.
1 parent 9ac7412 commit 6a164ec

11 files changed

+29
-215
lines changed

src/compiler/checker.ts

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21325,7 +21325,7 @@ namespace ts {
2132521325
}
2132621326
}
2132721327
// next line is copied from isDiscriminantProperty
21328-
return (checkFlags & CheckFlags.Discriminant) === CheckFlags.Discriminant && !maybeTypeOfKind(getUnionType(types), TypeFlags.Instantiable);;
21328+
return (checkFlags & CheckFlags.Discriminant) === CheckFlags.Discriminant && !maybeTypeOfKind(getUnionType(types), TypeFlags.Instantiable);
2132921329
}
2133021330

2133121331
// why it is not "!(declaredType.flags & TypeFlags.Union) || !isAccessExpression(expr)"
@@ -21384,7 +21384,7 @@ namespace ts {
2138421384
}
2138521385
// However, I still use declared type, and put it here. The main aim is for future improving.
2138621386
// We could deal with 'this' type and use ugly code to deal with Condition 2, then we could avoid use declared type at least for now
21387-
// passing all tests and issue #39110/#39114, but does it deserve? Is it clearly enough?
21387+
// passing all tests and issue #39110/#39114, but does it deserve? Is the code clear enough?
2138821388
return true;
2138921389
}
2139021390

@@ -21631,6 +21631,33 @@ namespace ts {
2163121631
// 2. use expression to mark wanted types as 'true'
2163221632
// 3. use the mark to get narrowed type from origional type.
2163321633

21634+
// If expression is a, return []
21635+
// If expression is a.b["c"].d(), return ["b","c","d"]
21636+
// NOTE: If element expression is not known in compile progress like a.b[f()].d, the result would be undefined
21637+
// //NOTE: this function need improvement, ElementAccessExpression argument might could be known in compile time, like "1"+"2", we should check "12" in the path, but how to get the value?
21638+
function getPropertyPathsOfAccessExpression(expressionOri: Expression, depth: number): __String[] | undefined {
21639+
const properties = [];
21640+
let expr: Expression = expressionOri;
21641+
let propName: __String;
21642+
while ((expr.kind === SyntaxKind.PropertyAccessExpression || expr.kind === SyntaxKind.ElementAccessExpression) && properties.length !== depth) {
21643+
if (expr.kind === SyntaxKind.PropertyAccessExpression) {
21644+
propName = (<PropertyAccessExpression>expr).name.escapedText;
21645+
}
21646+
else {
21647+
const argExpression = (<ElementAccessExpression>expr).argumentExpression;
21648+
if (isLiteralExpression(argExpression)) {
21649+
propName = escapeLeadingUnderscores(argExpression.text);
21650+
}
21651+
else {
21652+
return undefined;
21653+
}
21654+
}
21655+
properties.unshift(propName);
21656+
expr = (<PropertyAccessExpression>expr).expression;
21657+
}
21658+
return properties;
21659+
}
21660+
2163421661
function tryGetPropertyPathsOfReferenceFromExpression(expressionOri: Expression, _ref?: Node) {
2163521662
function getTypeDepthIfMatch(expression: Expression, ref: Node): number {
2163621663
let result = 0;
@@ -21648,33 +21675,6 @@ namespace ts {
2164821675
return -1;
2164921676
}
2165021677

21651-
// If expression is a, return []
21652-
// If expression is a.b["c"].d(), return ["b","c","d"]
21653-
// NOTE: If element expression is not known in compile progress like a.b[f()].d, the result would be undefined
21654-
// //NOTE: this function need improvement, ElementAccessExpression argument might could be known in compile time, like "1"+"2", we should check "12" in the path, but how to get the value?
21655-
function getPropertyPathsOfAccessExpression(expressionOri: Expression, depth: number): __String[] | undefined {
21656-
const properties = [];
21657-
let expr: Expression = expressionOri;
21658-
let propName: __String;
21659-
while ((expr.kind === SyntaxKind.PropertyAccessExpression || expr.kind === SyntaxKind.ElementAccessExpression) && properties.length !== depth) {
21660-
if (expr.kind === SyntaxKind.PropertyAccessExpression) {
21661-
propName = (<PropertyAccessExpression>expr).name.escapedText;
21662-
}
21663-
else {
21664-
const argExpression = (<ElementAccessExpression>expr).argumentExpression;
21665-
if (isLiteralExpression(argExpression)) {
21666-
propName = escapeLeadingUnderscores(argExpression.text);
21667-
}
21668-
else {
21669-
return undefined;
21670-
}
21671-
}
21672-
properties.unshift(propName);
21673-
expr = (<PropertyAccessExpression>expr).expression;
21674-
}
21675-
return properties;
21676-
}
21677-
2167821678
const depth = getTypeDepthIfMatch(expressionOri, reference);
2167921679
if (depth < 0) {
2168021680
return undefined;

tests/baselines/reference/typeGuardAccordingToProperty.errors.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -278,14 +278,4 @@ tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToProperty.ts(8
278278
let y = bar.y; // string
279279
}
280280
}
281-
282-
/////////////////////////////////////////////////////////
283-
// some case that need discuss further, This is not that right.
284-
function f1_(u: Union1) {
285-
const tmp1 = u.key;
286-
if (typeof tmp1 !== 'boolean') {
287-
u; //Union1
288-
u.key; //number | boolean
289-
}
290-
}
291281

tests/baselines/reference/typeGuardAccordingToProperty.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -256,16 +256,6 @@ function f3(bar: X) {
256256
let y = bar.y; // string
257257
}
258258
}
259-
260-
/////////////////////////////////////////////////////////
261-
// some case that need discuss further, This is not that right.
262-
function f1_(u: Union1) {
263-
const tmp1 = u.key;
264-
if (typeof tmp1 !== 'boolean') {
265-
u; //Union1
266-
u.key; //number | boolean
267-
}
268-
}
269259

270260

271261
//// [typeGuardAccordingToProperty.js]
@@ -464,12 +454,3 @@ function f3(bar) {
464454
var y = bar.y; // string
465455
}
466456
}
467-
/////////////////////////////////////////////////////////
468-
// some case that need discuss further, This is not that right.
469-
function f1_(u) {
470-
var tmp1 = u.key;
471-
if (typeof tmp1 !== 'boolean') {
472-
u; //Union1
473-
u.key; //number | boolean
474-
}
475-
}

tests/baselines/reference/typeGuardAccordingToProperty.symbols

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -700,29 +700,3 @@ function f3(bar: X) {
700700
}
701701
}
702702

703-
/////////////////////////////////////////////////////////
704-
// some case that need discuss further, This is not that right.
705-
function f1_(u: Union1) {
706-
>f1_ : Symbol(f1_, Decl(typeGuardAccordingToProperty.ts, 256, 1))
707-
>u : Symbol(u, Decl(typeGuardAccordingToProperty.ts, 260, 13))
708-
>Union1 : Symbol(Union1, Decl(typeGuardAccordingToProperty.ts, 51, 1))
709-
710-
const tmp1 = u.key;
711-
>tmp1 : Symbol(tmp1, Decl(typeGuardAccordingToProperty.ts, 261, 9))
712-
>u.key : Symbol(key, Decl(typeGuardAccordingToProperty.ts, 6, 20), Decl(typeGuardAccordingToProperty.ts, 11, 20), Decl(typeGuardAccordingToProperty.ts, 21, 19))
713-
>u : Symbol(u, Decl(typeGuardAccordingToProperty.ts, 260, 13))
714-
>key : Symbol(key, Decl(typeGuardAccordingToProperty.ts, 6, 20), Decl(typeGuardAccordingToProperty.ts, 11, 20), Decl(typeGuardAccordingToProperty.ts, 21, 19))
715-
716-
if (typeof tmp1 !== 'boolean') {
717-
>tmp1 : Symbol(tmp1, Decl(typeGuardAccordingToProperty.ts, 261, 9))
718-
719-
u; //Union1
720-
>u : Symbol(u, Decl(typeGuardAccordingToProperty.ts, 260, 13))
721-
722-
u.key; //number | boolean
723-
>u.key : Symbol(key, Decl(typeGuardAccordingToProperty.ts, 6, 20), Decl(typeGuardAccordingToProperty.ts, 11, 20), Decl(typeGuardAccordingToProperty.ts, 21, 19))
724-
>u : Symbol(u, Decl(typeGuardAccordingToProperty.ts, 260, 13))
725-
>key : Symbol(key, Decl(typeGuardAccordingToProperty.ts, 6, 20), Decl(typeGuardAccordingToProperty.ts, 11, 20), Decl(typeGuardAccordingToProperty.ts, 21, 19))
726-
}
727-
}
728-

tests/baselines/reference/typeGuardAccordingToProperty.types

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -774,31 +774,3 @@ function f3(bar: X) {
774774
}
775775
}
776776

777-
/////////////////////////////////////////////////////////
778-
// some case that need discuss further, This is not that right.
779-
function f1_(u: Union1) {
780-
>f1_ : (u: Union1) => void
781-
>u : Union1
782-
783-
const tmp1 = u.key;
784-
>tmp1 : number | boolean
785-
>u.key : number | boolean
786-
>u : Union1
787-
>key : number | boolean
788-
789-
if (typeof tmp1 !== 'boolean') {
790-
>typeof tmp1 !== 'boolean' : boolean
791-
>typeof tmp1 : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
792-
>tmp1 : number | boolean
793-
>'boolean' : "boolean"
794-
795-
u; //Union1
796-
>u : Union1
797-
798-
u.key; //number | boolean
799-
>u.key : number | boolean
800-
>u : Union1
801-
>key : number | boolean
802-
}
803-
}
804-

tests/baselines/reference/typeGuardAccordingToPropertyBySwitch.errors.txt

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,4 @@ tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyBySwi
194194
break;
195195
}
196196
}
197-
198-
/////////////////////////////////////////////////////////
199-
// some case that need discuss further, This is not that right.
200-
function f1_(u: Union1) {
201-
const tmp1 = u.key;
202-
if (typeof tmp1 !== 'boolean') {
203-
u; //Union1
204-
u.key; //number | boolean
205-
}
206-
}
207197

tests/baselines/reference/typeGuardAccordingToPropertyBySwitch.js

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -177,16 +177,6 @@ function f3(bar: X) {
177177
break;
178178
}
179179
}
180-
181-
/////////////////////////////////////////////////////////
182-
// some case that need discuss further, This is not that right.
183-
function f1_(u: Union1) {
184-
const tmp1 = u.key;
185-
if (typeof tmp1 !== 'boolean') {
186-
u; //Union1
187-
u.key; //number | boolean
188-
}
189-
}
190180

191181

192182
//// [typeGuardAccordingToPropertyBySwitch.js]
@@ -309,12 +299,3 @@ function f3(bar) {
309299
break;
310300
}
311301
}
312-
/////////////////////////////////////////////////////////
313-
// some case that need discuss further, This is not that right.
314-
function f1_(u) {
315-
var tmp1 = u.key;
316-
if (typeof tmp1 !== 'boolean') {
317-
u; //Union1
318-
u.key; //number | boolean
319-
}
320-
}

tests/baselines/reference/typeGuardAccordingToPropertyBySwitch.symbols

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -389,29 +389,3 @@ function f3(bar: X) {
389389
}
390390
}
391391

392-
/////////////////////////////////////////////////////////
393-
// some case that need discuss further, This is not that right.
394-
function f1_(u: Union1) {
395-
>f1_ : Symbol(f1_, Decl(typeGuardAccordingToPropertyBySwitch.ts, 177, 1))
396-
>u : Symbol(u, Decl(typeGuardAccordingToPropertyBySwitch.ts, 181, 13))
397-
>Union1 : Symbol(Union1, Decl(typeGuardAccordingToPropertyBySwitch.ts, 51, 1))
398-
399-
const tmp1 = u.key;
400-
>tmp1 : Symbol(tmp1, Decl(typeGuardAccordingToPropertyBySwitch.ts, 182, 9))
401-
>u.key : Symbol(key, Decl(typeGuardAccordingToPropertyBySwitch.ts, 6, 20), Decl(typeGuardAccordingToPropertyBySwitch.ts, 11, 20), Decl(typeGuardAccordingToPropertyBySwitch.ts, 21, 19))
402-
>u : Symbol(u, Decl(typeGuardAccordingToPropertyBySwitch.ts, 181, 13))
403-
>key : Symbol(key, Decl(typeGuardAccordingToPropertyBySwitch.ts, 6, 20), Decl(typeGuardAccordingToPropertyBySwitch.ts, 11, 20), Decl(typeGuardAccordingToPropertyBySwitch.ts, 21, 19))
404-
405-
if (typeof tmp1 !== 'boolean') {
406-
>tmp1 : Symbol(tmp1, Decl(typeGuardAccordingToPropertyBySwitch.ts, 182, 9))
407-
408-
u; //Union1
409-
>u : Symbol(u, Decl(typeGuardAccordingToPropertyBySwitch.ts, 181, 13))
410-
411-
u.key; //number | boolean
412-
>u.key : Symbol(key, Decl(typeGuardAccordingToPropertyBySwitch.ts, 6, 20), Decl(typeGuardAccordingToPropertyBySwitch.ts, 11, 20), Decl(typeGuardAccordingToPropertyBySwitch.ts, 21, 19))
413-
>u : Symbol(u, Decl(typeGuardAccordingToPropertyBySwitch.ts, 181, 13))
414-
>key : Symbol(key, Decl(typeGuardAccordingToPropertyBySwitch.ts, 6, 20), Decl(typeGuardAccordingToPropertyBySwitch.ts, 11, 20), Decl(typeGuardAccordingToPropertyBySwitch.ts, 21, 19))
415-
}
416-
}
417-

tests/baselines/reference/typeGuardAccordingToPropertyBySwitch.types

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -421,31 +421,3 @@ function f3(bar: X) {
421421
}
422422
}
423423

424-
/////////////////////////////////////////////////////////
425-
// some case that need discuss further, This is not that right.
426-
function f1_(u: Union1) {
427-
>f1_ : (u: Union1) => void
428-
>u : Union1
429-
430-
const tmp1 = u.key;
431-
>tmp1 : number | boolean
432-
>u.key : number | boolean
433-
>u : Union1
434-
>key : number | boolean
435-
436-
if (typeof tmp1 !== 'boolean') {
437-
>typeof tmp1 !== 'boolean' : boolean
438-
>typeof tmp1 : "string" | "number" | "bigint" | "boolean" | "symbol" | "undefined" | "object" | "function"
439-
>tmp1 : number | boolean
440-
>'boolean' : "boolean"
441-
442-
u; //Union1
443-
>u : Union1
444-
445-
u.key; //number | boolean
446-
>u.key : number | boolean
447-
>u : Union1
448-
>key : number | boolean
449-
}
450-
}
451-

tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToProperty.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -257,13 +257,3 @@ function f3(bar: X) {
257257
let y = bar.y; // string
258258
}
259259
}
260-
261-
/////////////////////////////////////////////////////////
262-
// some case that need discuss further, This is not that right.
263-
function f1_(u: Union1) {
264-
const tmp1 = u.key;
265-
if (typeof tmp1 !== 'boolean') {
266-
u; //Union1
267-
u.key; //number | boolean
268-
}
269-
}

tests/cases/conformance/expressions/typeGuards/typeGuardAccordingToPropertyBySwitch.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -178,13 +178,3 @@ function f3(bar: X) {
178178
break;
179179
}
180180
}
181-
182-
/////////////////////////////////////////////////////////
183-
// some case that need discuss further, This is not that right.
184-
function f1_(u: Union1) {
185-
const tmp1 = u.key;
186-
if (typeof tmp1 !== 'boolean') {
187-
u; //Union1
188-
u.key; //number | boolean
189-
}
190-
}

0 commit comments

Comments
 (0)