Skip to content

Commit

Permalink
Support top-level await - fixes #1 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Sep 5, 2018
1 parent 0bc1413 commit bb1e01f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 1 deletion.
12 changes: 12 additions & 0 deletions readme.md
Expand Up @@ -61,6 +61,18 @@ If we don't change the test file and we run the `tsd-check` command again, the t

<img src="screenshot.png" width="1330">

### Top-level `await`

If your method returns a `Promise`, you can use top-level `await` to resolve the value instead of wrapping it in an `async` [IIFE](https://developer.mozilla.org/en-US/docs/Glossary/IIFE).

```ts
import {expectType} from 'tsd-check';
import concat from '.';

expectType<Promise<string>>(concat('foo', 'bar'));

expectType<string>(await concat('foo', 'bar'));
```


## Assertions
Expand Down
7 changes: 6 additions & 1 deletion source/lib/compiler.ts
Expand Up @@ -2,6 +2,11 @@ import * as path from 'path';
import {createProgram, getPreEmitDiagnostics, ScriptTarget, ModuleResolutionKind, flattenDiagnosticMessageText} from 'typescript';
import {Diagnostic, Context} from './interfaces';

// List of diagnostic codes that should be ignored
const ignoredDiagnostics = new Set<number>([
1308 // Support top-level `await`
]);

const loadConfig = () => {
return {
moduleResolution: ModuleResolutionKind.NodeJs,
Expand All @@ -28,7 +33,7 @@ export const getDiagnostics = (context: Context): Diagnostic[] => {
const result: Diagnostic[] = [];

for (const diagnostic of diagnostics) {
if (!diagnostic.file) {
if (!diagnostic.file || ignoredDiagnostics.has(diagnostic.code)) {
continue;
}

Expand Down
6 changes: 6 additions & 0 deletions source/test/fixtures/top-level-await/index.d.ts
@@ -0,0 +1,6 @@
declare const one: {
(foo: string, bar: string): Promise<string>;
(foo: number, bar: number): Promise<number>;
};

export default one;
3 changes: 3 additions & 0 deletions source/test/fixtures/top-level-await/index.js
@@ -0,0 +1,3 @@
module.exports.default = (foo, bar) => {
return foo + bar;
};
5 changes: 5 additions & 0 deletions source/test/fixtures/top-level-await/index.test-d.ts
@@ -0,0 +1,5 @@
import {expectType} from '../../..';
import one from '.';

expectType<string>(await one('foo', 'bar'));
expectType<number>(await one(1, 2));
7 changes: 7 additions & 0 deletions source/test/fixtures/top-level-await/package.json
@@ -0,0 +1,7 @@
{
"name": "foo",
"files": [
"index.js",
"index.d.ts"
]
}
6 changes: 6 additions & 0 deletions source/test/test.ts
Expand Up @@ -36,3 +36,9 @@ test('return no diagnostics', async t => {

t.true(diagnostics.length === 0);
});

test('support top-level await', async t => {
const diagnostics = await m({cwd: path.join(__dirname, 'fixtures/top-level-await')});

t.true(diagnostics.length === 0);
});

0 comments on commit bb1e01f

Please sign in to comment.