Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

# Object.isPartOfTypeNode crashes with TypeError: Cannot read properties of undefined (reading 'kind')

See original GitHub issue

# Bug Report

### 🔎 Search Terms

Object.isPartOfTypeNode and TypeError: Cannot read properties of undefined

### 🕗 Version & Regression Information

• This is a crash
• This is the behaviour in every version I tried (4.7.3, 4.7.4, next and nightly which was 4.8.0-dev.20220804 for me), and I reviewed the FAQ for entries about tuple, recursion, tail, elimitation, depth and limitation
• I was unable to test this on prior versions because I use 4.7.3 in my project

### 💻 Code

I reduced the codebase of ttuple to 64 lines.

type DigitMapping<T> = {
"0": [];
"1": [T];
"2": [T, T];
"3": [T, T, T];
"4": [T, T, T, T];
"5": [T, T, T, T, T];
"6": [T, T, T, T, T, T];
"7": [T, T, T, T, T, T, T];
"8": [T, T, T, T, T, T, T, T];
"9": [T, T, T, T, T, T, T, T, T];
};

type Multiply10<T extends AnyArray> = [
...T,
...T,
...T,
...T,
...T,
...T,
...T,
...T,
...T,
...T
];

type Add<N1 extends AnyArray, N2 extends AnyArray> = [...N1, ...N2];

type _ToTuple<
V,
S extends string,
T extends AnyArray = []
> = S extends `\${infer D}\${infer Rest}`
? _ToTuple<
V,
Rest,
>
: [...T, ...V[], ...T];

type ToTuple<V, S extends string> = _ToTuple<V, S>;

type ElementOf<T extends AnyArray> = T extends readonly (infer V)[] ? V : never;

const length = <
T extends AnyArray,
S extends `\${number}`,
R = ToTuple<ElementOf<T>, S>
>(
array: T,
condition: `>= \${S}`
): array is R extends T ? R : never => {
const expectedLength = Number(condition.split(" ")[1]);

if (array.length >= expectedLength) {
return true;
}

return false;
};

export { length };

Also the code is available in the branch here – https://github.com/Beraliv/ttuple/pull/22

### 🙁 Actual behaviour

➜ npm run build

> ttuple@0.2.1 build
> tsc --project tsconfig.json

/Users/beraliv/Documents/Code/ttuple/node_modules/typescript/lib/tsc.js:96887
throw e;
^

at Object.isPartOfTypeNode (/Users/beraliv/Documents/Code/ttuple/node_modules/typescript/lib/tsc.js:11787:25)
at _loop_15 (/Users/beraliv/Documents/Code/ttuple/node_modules/typescript/lib/tsc.js:50429:51)
at createNormalizedTupleType (/Users/beraliv/Documents/Code/ttuple/node_modules/typescript/lib/tsc.js:50445:31)
at createNormalizedTypeReference (/Users/beraliv/Documents/Code/ttuple/node_modules/typescript/lib/tsc.js:50398:45)
at instantiateTypeWorker (/Users/beraliv/Documents/Code/ttuple/node_modules/typescript/lib/tsc.js:52755:77)
at instantiateTypeWithAlias (/Users/beraliv/Documents/Code/ttuple/node_modules/typescript/lib/tsc.js:52740:26)
at instantiateType (/Users/beraliv/Documents/Code/ttuple/node_modules/typescript/lib/tsc.js:52726:37)
at getMappedType (/Users/beraliv/Documents/Code/ttuple/node_modules/typescript/lib/tsc.js:52449:63)
at /Users/beraliv/Documents/Code/ttuple/node_modules/typescript/lib/tsc.js:52709:92
at Object.map (/Users/beraliv/Documents/Code/ttuple/node_modules/typescript/lib/tsc.js:367:29)

### 🙂 Expected behaviour

➜ npm run build

> ttuple@0.2.1 build
> tsc --project tsconfig.json

If I replace ToTuple with _ToTuple, it starts working 🤯

As I’ve seen example with GetChar in release notes of TS 4.5, it should be fine to declare it this way – https://devblogs.microsoft.com/typescript/announcing-typescript-4-5/#tailrec-conditional

I connected the project to node debugger and could see that in _loop_15 9th element was in the branch of tuple type (after check of isTupleType), but the length of elements.length + expandedTypes.length is 11100 (not sure what it means) so it tried to throw an error, but currentNode was undefined

### Issue Analytics

• State:
• Created a year ago

2reactions
whzx5bybcommented, Aug 5, 2022

Here’s a simplified repo in 4.7.4 workbench. You can see the error in the devtools console.

// @filename: index.ts
import { ToTuple } from "./helper";

export const fn = function<S extends string>(foo: ToTuple<any, S>): void {

}

// @filename: helper.ts
type _ToTuple<
V,
S extends string,
T extends any[]
> = S extends `\${infer H}\${infer Rest}`
? _ToTuple<
V,
Rest,
[...[
...T,
...T,
...T,
...T,
...T,
...T,
...T,
...T,
...T,
], 0]
>
: never;

export type ToTuple<V, S extends string> = _ToTuple<V, S, []>;

The behavior is quite weird. And here is some ways I found to make the error disappear.

• Merge the two files into a single, as @Beraliv mentioned in comment.
• Do not export fn.
• Use function declaration for fn, not function expression.
• Export _ToTuple, even it’s not imported anywhere.
• Reduce the times of repeated ...T, to 3.
• Remove the element 0 following the repeated ...T,.
• Remove the first unused type parameter V in _ToTuple.
1reaction
Beralivcommented, Aug 24, 2022

You are welcome to give it a try by following the process in https://github.com/microsoft/TypeScript/blob/main/CONTRIBUTING.md, but I have no idea how challenging it will be, and we may not be able to give you much support if you get stuck—if you end up solving it I’ll merge it, but if it seems like it will be quicker for me to just start fresh, that’s what I’ll do. That’s in contrast to “Help Wanted” and “Good First Issue” issues, where I try to offer more guidance. So you might want to look for one of those if you just want to contribute in general, but if you’re set on this issue, feel free, but at your own risk 🙂

It sounds reasonable

I will start from “Help Wanted” and “Good First Issue” first, then will jump on this one, if it’s working for you

#### Top Results From Across the Web

getTypeFromTypeNodeWorker crashes with TypeError ...
getTypeFromTypeNodeWorker crashes with TypeError: Cannot read properties of undefined (reading 'kind') #50479.
Cannot Read Property of Undefined in JavaScript - Rollbar
TypeError : Cannot read property of undefined occurs when a property is read or a function is called on an undefined variable.
Please paste code from the component that you have created. As you can see, angular is trying to read property "kind" from some...
How to Prevent the Error: Cannot Read Property '0' of Undefined
A nonexistent key in an object returns undefined ; thus, trying to access its elements throws the Cannot read property '0' of undefined...
Avoiding those dang cannot read property of undefined errors
Uncaught TypeError: Cannot read property 'foo' of undefined.​ The dreaded error we all hit at some point in JavaScript development.

#### Troubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free