microsoft / dtslint

A utility built on TSLint for linting TypeScript declaration (.d.ts) files.
MIT License
925 stars 86 forks source link

Type union comes out as "any" #179

Open danvk opened 5 years ago

danvk commented 5 years ago

If I add this to expectType.ts.lint:

// $ExpectType "foo" | "bar"
declare let t: 'foo' | 'bar';

and run npm run build && npm run test, then I get the following error:

  // $ExpectType "foo" | "bar"
  declare let t: 'foo' | 'bar';

- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Expected type to be:
-   "foo" | "bar"
- got:
-   any]

Somehow the type union gets converted to "any". Putting that same code in the TypeScript playground shows that TS understands the type correctly. I'm not sure what's going on here.

image

danvk commented 5 years ago

Here are a few variations that do work:

function test(t: 'foo' | 'bar') {
  t; // $ExpectType "foo" | "bar"
}

let t: 'foo' | 'bar';
t;  // $ExpectType "foo" | "bar"

So I guess you just can't do a type assertion on the line in which a variable is declared? It has to be on an expression?