Skip to content

Commit 5594a03

Browse files
committed
Correctly handled mapped types from other types
Fixes #16
1 parent 94b84bc commit 5594a03

File tree

3 files changed

+45
-5
lines changed

3 files changed

+45
-5
lines changed

src/transformer.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
287287
// tslint:disable-next-line:cyclomatic-complexity
288288
function isTypePropertyExternal(type: ts.Type, typePropertyName: string): boolean {
289289
const symbol = type.getSymbol();
290+
const propertySymbol = typeChecker.getPropertyOfType(type, typePropertyName);
290291

291292
if (type.flags & ts.TypeFlags.Object) {
292293
const objectType = type as ts.ObjectType;
@@ -295,11 +296,13 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
295296
return true;
296297
}
297298

298-
// because of we can't get where a property come from in mapped types
299-
// let's check the whole its type explicitly
300-
// thus let's treat any property of a mapped type as "external" if its parent type is external
299+
// in case when we can't get where a property come from in mapped types
300+
// let's check the whole type explicitly
301+
// thus in case of when property doesn't have a declaration let's treat any property of a mapped type as "external" if its parent type is external
302+
// e.g. Readonly<Foo>.field will look for `field` in _Foo_ type (not in Readonly<Foo>), but { [K in 'foo' | 'bar']: any } won't
301303
// perhaps it would be awesome to handle exactly property we have, but ¯\_(ツ)_/¯
302-
if ((objectType.objectFlags & ts.ObjectFlags.Mapped) && symbol !== undefined && getSymbolVisibilityType(symbol) === VisibilityType.External) {
304+
const propertyHasDeclarations = propertySymbol !== undefined ? getDeclarationsForSymbol(propertySymbol).length !== 0 : false;
305+
if ((objectType.objectFlags & ts.ObjectFlags.Mapped) && symbol !== undefined && !propertyHasDeclarations && getSymbolVisibilityType(symbol) === VisibilityType.External) {
303306
return true;
304307
}
305308
}
@@ -328,7 +331,6 @@ function createTransformerFactory(program: ts.Program, options?: Partial<RenameO
328331
}
329332
}
330333

331-
const propertySymbol = typeChecker.getPropertyOfType(type, typePropertyName);
332334
if (propertySymbol === undefined) {
333335
return false;
334336
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
interface Foo {
2+
field: string;
3+
field2: number;
4+
}
5+
6+
type ReadonlyFoo = Readonly<Foo>;
7+
8+
function bar(foo: ReadonlyFoo): void {
9+
console.log(foo.field, foo.field2);
10+
}
11+
12+
function baz(): void {
13+
const foo: Foo = {
14+
field: '',
15+
field2: 0,
16+
};
17+
18+
bar(foo);
19+
20+
bar({
21+
field: '',
22+
field2: 0,
23+
});
24+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function bar(foo) {
2+
console.log(foo._internal_field, foo._internal_field2);
3+
}
4+
function baz() {
5+
var foo = {
6+
_internal_field: '',
7+
_internal_field2: 0,
8+
};
9+
bar(foo);
10+
bar({
11+
_internal_field: '',
12+
_internal_field2: 0,
13+
});
14+
}

0 commit comments

Comments
 (0)