Skip to content

Commit 9f95513

Browse files
author
Evgeniy Timokhov
committed
Partially fixed handling any/unknown types so now variables of these types are treated as public
But sequential "casting" is not handling yet (obj as any as any as InternalInterface)
1 parent 69acca7 commit 9f95513

File tree

5 files changed

+53
-2
lines changed

5 files changed

+53
-2
lines changed

src/transformer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,13 +286,18 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
286286

287287
// tslint:disable-next-line:cyclomatic-complexity
288288
function isTypePropertyExternal(type: ts.Type, typePropertyName: string): boolean {
289-
const symbol = type.getSymbol();
290-
const propertySymbol = typeChecker.getPropertyOfType(type, typePropertyName);
289+
// if a type is unknown or any - they should be interpret as a public ones
290+
if (type.flags & ts.TypeFlags.Unknown || type.flags & ts.TypeFlags.Any) {
291+
return true;
292+
}
291293

292294
if (type.flags & ts.TypeFlags.IndexedAccess) {
293295
return isTypePropertyExternal(typeChecker.getApparentType(type), typePropertyName);
294296
}
295297

298+
const symbol = type.getSymbol();
299+
const propertySymbol = typeChecker.getPropertyOfType(type, typePropertyName);
300+
296301
if (type.flags & ts.TypeFlags.Object) {
297302
const objectType = type as ts.ObjectType;
298303
// treat any tuple property as "external"

tests/test-cases/any/input.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,14 @@ interface PublicByJSDocInterface {
1414
function func(obj1: PublicInterface, obj2: InternalInterface, obj3: PublicByJSDocInterface): void {
1515
console.log(obj1.field.f1Any, obj2.field.f2Any, obj3.field.f3Any);
1616
}
17+
18+
function func2(): any {
19+
const b = { value: 321 } as any;
20+
const c = { field: 222 } as any as InternalInterface;
21+
return {
22+
foo: 1,
23+
bar: b,
24+
baz: 3,
25+
c: c,
26+
};
27+
}

tests/test-cases/any/output.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
33
function func(obj1, obj2, obj3) {
44
console.log(obj1.field.f1Any, obj2._internal_field.f2Any, obj3.field.f3Any);
55
}
6+
function func2() {
7+
var b = { value: 321 };
8+
var c = { _internal_field: 222 };
9+
return {
10+
foo: 1,
11+
bar: b,
12+
baz: 3,
13+
c: c,
14+
};
15+
}

tests/test-cases/unknown/input.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
interface InternalInterface {
2+
int: number;
3+
}
4+
5+
function func(): unknown {
6+
const b = { value: 321 } as unknown;
7+
const c = { int: 222 } as unknown as InternalInterface;
8+
9+
return {
10+
foo: 1,
11+
bar: b,
12+
baz: 3,
13+
c: c,
14+
};
15+
}

tests/test-cases/unknown/output.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
function func() {
2+
var b = { value: 321 };
3+
var c = { _internal_int: 222 };
4+
return {
5+
foo: 1,
6+
bar: b,
7+
baz: 3,
8+
c: c,
9+
};
10+
}

0 commit comments

Comments
 (0)