tsdjs / tsd

Check TypeScript type definitions
MIT License
2.4k stars 67 forks source link

0.24.0 Regression Test #164

Closed tommy-mitchell closed 2 years ago

tommy-mitchell commented 2 years ago

Refactors the parser as mentioned here in #161 and adds a regression test:

const tag = (strings: TemplateStringsArray, ...args: any[]) => {
  let value = strings[0];
  let i = 0;

  while (i < args.length) {
    let s: string;

    if (Array.isArray(args[i])) {
      // PROBLEM: Identifiers `map` and `join` have no Symbols
      // TS should infer `args[i]` as `Array<any>`, but doesn't
      // `checker.getSymbolAtLocation(expression)` is undefined
      s = args[i].map((x: any) => `${x}`).join(' ');
    } else {
      s = `${args[i]}`;
    }

    value += s;
  }

  return value;
};

// fails with `Cannot read properties of undefined (reading 'flags')` in 0.24.0
expectType<string>(tag`foo`);

(AST Viewer). Adapted from zx core.ts.

I'm not sure why tsd is parsing the source code of zx instead of just the test-d files. I wasn't able to reproduce this in a test case.

Having a Node but not a Symbol means that the given expression is syntactically-correct, but not semantically-correct. Ultimately, I was wrong to assume that maybeAlias would be non-null and it's safer to have this check.

tommy-mitchell commented 2 years ago

Made the test simpler:

// Identifier `bar` has no Symbol
const anyCall = (foo: any) => foo.bar();

// Fails with `Cannot read properties of undefined (reading 'flags')` in 0.24.0
expectType<any>(anyCall('foo'));