timocov / ts-transformer-properties-rename

TypeScript custom transformer to rename properties
MIT License
70 stars 3 forks source link

Incorrect handing `in` operator with string literal #5

Closed timocov closed 4 years ago

timocov commented 4 years ago

Bug report

Input code

export interface ExportedInterface {
    foo: number;
}

export interface AnotherExportedInterface {
    bar: number;
    isAnother: boolean;
}

export type ExportedType = ExportedInterface | AnotherExportedInterface;

interface NonExportedInterface {
    foo: number;
}

interface AnotherNonExportedInterface {
    bar: number;
    isAnother: boolean;
}

type NonExportedType = NonExportedInterface | AnotherNonExportedInterface;

declare function getNonExported(): NonExportedType;
declare const moveEvent: MouseEvent | TouchEvent;

export function func(type: ExportedType): void {
    if ('isAnother' in type) {
        console.log(type.bar);
    } else {
        console.log(type.foo);
    }

    const nonExportedVar = getNonExported();
    if ('isAnother' in nonExportedVar) {
        console.log(nonExportedVar.bar);
    } else {
        console.log(nonExportedVar.foo);
    }

    if ('onorientationchange' in window || 'button' in moveEvent) {
        console.log('check');
    }
}

Expected output

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function func(type) {
    if ('isAnother' in type) {
        console.log(type.bar);
    }
    else {
        console.log(type.foo);
    }
    var nonExportedVar = getNonExported();
    if ('_internal_isAnother' in nonExportedVar) {
        console.log(nonExportedVar._internal_bar);
    }
    else {
        console.log(nonExportedVar._internal_foo);
    }
    if ('onorientationchange' in window || 'button' in moveEvent) {
        console.log('check');
    }
}
exports.func = func;

Actual output

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function func(type) {
    if ('isAnother' in type) {
        console.log(type.bar);
    }
    else {
        console.log(type.foo);
    }
    var nonExportedVar = getNonExported();
    if ('isAnother' in nonExportedVar) {
        console.log(nonExportedVar._internal_bar);
    }
    else {
        console.log(nonExportedVar._internal_foo);
    }
    if ('onorientationchange' in window || 'button' in moveEvent) {
        console.log('check');
    }
}
exports.func = func;