Skip to content
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

typscript async/await not working #3274

Closed
wedelgaard opened this issue Dec 19, 2017 · 10 comments
Closed

typscript async/await not working #3274

wedelgaard opened this issue Dec 19, 2017 · 10 comments
Labels
type: bug An issue or pull request relating to a bug in Gatsby

Comments

@wedelgaard
Copy link

It seems like async/await it not working when using gatsby-plugin-typescript :( The following error is thrown:

{my path}/gatsby-site/src/pages/index.tsx Unexpected token (13:26) You may need an appropriate loader to handle this file type.

this is how the pages/index.tsx looks like. It works without any problems if index.tsx is renamed to index.js (not typescript), but this is not desirable.
`
import * as React from 'react'
import Link from 'gatsby-link'

const asyncFunc = () => {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve('done!')
}, 1000)
})
}

const getStuff = async () => {
return await asyncFunc()
}

const IndexPage = (props: any) => {

const test = getStuff()

return (
...something
)
}

export default IndexPage
`

@jquense
Copy link
Contributor

jquense commented Dec 19, 2017

does async/await work normally with typescript? I'm not super familiar with it, perhasp you need to set the right config options?

@wedelgaard
Copy link
Author

@jquense should be supported from v. 1.7

https://blogs.msdn.microsoft.com/typescript/2015/11/03/what-about-asyncawait/

I'm using typescript v. 2.6.1

@wedelgaard
Copy link
Author

@calcsam
Copy link
Contributor

calcsam commented Dec 26, 2017

Can you add an example repo? Assume you are using https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-plugin-typescript? Can you try inserting await in https://github.com/gatsbyjs/gatsby/tree/master/examples/using-typescript and seeing if you can repro?

@wedelgaard
Copy link
Author

wedelgaard commented Dec 26, 2017

@calcsam here's a repo demonstrating it https://github.com/wedelgaard/gatsby-typescript-starter

@calcsam calcsam added the type: bug An issue or pull request relating to a bug in Gatsby label Dec 30, 2017
@calcsam
Copy link
Contributor

calcsam commented Dec 30, 2017

Thanks for the repro! On that repo for me locally gatsby-build fails but gatsby-develop succeeds. Will label this as a bug, someone more familiar than me with TS syntax should probably take a look :)

@farcaller
Copy link
Contributor

Can reproduce here too, same problem as #3356 I believe. TL;DR: TS is set to transpileOnly by default, which means it will check the types and return the source file sans TS semantics. Given that there's no babel pipeline to process it further the other code is effectively dealing with esnext and chokes up on it.

That said, transpileOnly: false is broken.

@mohsen1
Copy link

mohsen1 commented Feb 11, 2018

I don't understand why target exnext is forced here?

// overwrite defaults with custom compiler options
const copts = Object.assign({}, compilerDefaults, compilerOptions, {
target: `esnext`,
module: `es6`,
})

if the typescript plugin allows specifying target from outside we can resolve this issue by specifying a lower level target like es5 in tsconfig

@farcaller
Copy link
Contributor

I don't understand why target exnext is forced here?

I believe the tagged literals appeared only in es2015, so you can go down at most to es2015. It doesn't make much sense as babel is supposed to handle es2015..esnext difference too, though.

@KyleAMathews
Copy link
Contributor

Due to the high volume of issues, we're closing out older ones without recent activity. Please open a new issue if you need help!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug An issue or pull request relating to a bug in Gatsby
Projects
None yet
Development

No branches or pull requests

6 participants