Skip to content

tsc crashes resolving a type of computed property in destructiring assignment #26321

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

Closed
pablobirukov opened this issue Aug 9, 2018 · 4 comments
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@pablobirukov
Copy link

TypeScript Version: 3.1.0-dev.20180809, 3.0.1

Search Terms:
Cannot read property 'charCodeAt' of undefined

Code

interface Limit1 {
    propName: { min: number; max: number };
}
interface Limit2 {
    propName: { min: number; max: number };
}

type Limit = Limit1 | Limit2; // should be Union type

export interface Meta {
    propName: "propName";
}

function foo (meta: Meta, limits: Limit): void {
    const {
        [`propName`]: { min: minBonus, max: maxBonus }, // should be computed
    } = limits;
}

Expected behavior:
No crash, code compiled

Actual behavior:

Cannot read property 'charCodeAt' of undefined
.../node_modules/typescript/lib/tsc.js:68035
                throw e;
                ^

TypeError: Cannot read property 'charCodeAt' of undefined
at isLateBoundName (.../node_modules/typescript/lib/tsc.js:28756:25)
at createUnionOrIntersectionProperty (.../node_modules/typescript/lib/tsc.js:29638:38)
at getUnionOrIntersectionProperty (.../node_modules/typescript/lib/tsc.js:29699:28)
at getPropertyOfUnionOrIntersectionType (.../node_modules/typescript/lib/tsc.js:29707:28)
at getPropertyOfType (.../node_modules/typescript/lib/tsc.js:29727:24)
at checkVariableLikeDeclaration (.../node_modules/typescript/lib/tsc.js:43025:36)
at checkBindingElement (.../node_modules/typescript/lib/tsc.js:43123:20)
at checkSourceElement (.../node_modules/typescript/lib/tsc.js:44827:28)
at Object.forEach (.../node_modules/typescript/lib/tsc.js:168:30)
at checkVariableLikeDeclaration (.../node_modules/typescript/lib/tsc.js:43036:20)

Playground Link:

Note, that the code is not compiled

@ghost ghost added the Bug A bug in TypeScript label Aug 9, 2018
@ghost
Copy link

ghost commented Aug 9, 2018

declare const ab: { n: number } | { n: string };
const { [`n`]: n } = ab;

@ghost
Copy link

ghost commented Aug 9, 2018

checkVariableLikeDeclaration calls getPropertyOfType using getTextOfPropertyName(name), but that is undefined for a computed name. (See #18217 comment in getTextOfPropertyName.) To do this properly we need a function that can find a property for an indexed access. That could also fix #26328.

@ghost ghost added the Fixed A PR has been merged for this issue label Aug 9, 2018
@RyanCavanaugh RyanCavanaugh assigned ghost Aug 9, 2018
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 3.1 milestone Aug 9, 2018
@ghost ghost closed this as completed in #26334 Aug 9, 2018
@martpie
Copy link

martpie commented Aug 27, 2018

@Andy-MS Has it been released yet? I am facing this issue too.

Or can you recommend a way to mitigate it in the meantime?

@ghost
Copy link

ghost commented Aug 27, 2018

Yes, should be released in typescript@next.

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

3 participants