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

TS 4.7 syntax regression: ';' expected. #48711

Closed
MichaelMitchell-at opened this issue Apr 14, 2022 · 10 comments Β· Fixed by #48755
Closed

TS 4.7 syntax regression: ';' expected. #48711

MichaelMitchell-at opened this issue Apr 14, 2022 · 10 comments Β· Fixed by #48755
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@MichaelMitchell-at
Copy link

Bug Report

πŸ”Ž Search Terms

typeof syntax missing semicolon generic

πŸ•— Version & Regression Information

This changed between versions 4.6.2 and 4.7.0-beta

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

This is a minimal repro, for a real-world example of a breakage, see https://raw.githubusercontent.com/pmndrs/react-spring/master/packages/types/interpolation.d.ts

interface Example {
  (a: number): typeof a

  <T>(): void
}

πŸ™ Actual behavior

Syntax errors:

Type 'number' has no signatures for which the type argument list is applicable.

Return type of call signature from exported interface has or is using private name 'T'.

';' expected.

πŸ™‚ Expected behavior

No syntax errors

@Josh-Cena
Copy link
Contributor

This is new ASI hazard introduced by instantiation expressions, because typeof a<T> is now a valid parse.

@MichaelMitchell-at
Copy link
Author

This is new ASI hazard introduced by instantiation expressions, because typeof a<T> is now a valid parse.

I'm surprised that instantiation expression syntax can be used in a type context.

@Josh-Cena
Copy link
Contributor

That's one of the chief purposes of instantiation expressions. For example, you can do ReturnType<typeof fn<string>> to get string from fn: <T>() => T extends string ? string : number

@RyanCavanaugh RyanCavanaugh added Working as Intended The behavior described is the intended behavior; this is not a bug Breaking Change Would introduce errors in existing code labels Apr 15, 2022
@RyanCavanaugh
Copy link
Member

We need to add this to the blog post

@MichaelMitchell-at
Copy link
Author

MichaelMitchell-at commented Apr 15, 2022

Just going to throw out an overly-wishful idea that could maybe save the TS community a lot of effort when breaking syntax changes come out. What if:

  • the TS compiler shipped with a set of transforms to convert old supported syntax into new supported syntax, i.e. the opposite of downlevel-dts.
  • use typesVersions in package.json to detect what TS versions a package was originally written for (maybe fallback to using package release date as a heuristic since most packages don't specify typesVersions)
  • when tsc typechecks your project it automatically transforms third-party package files in-memory using the information from above when type-checking them

@RyanCavanaugh
Copy link
Member

Just going to throw out an overly-wishful idea...

To set expectations, I don't think we were even aware of the fact that this was a breaking change πŸ™ƒ. It's quite rare for this to happen.

@MichaelMitchell-at
Copy link
Author

True, though something like that idea would also be really helpful for enabling exactOptionalPropertyTypes, since it's really difficult when third-party types are written with exactOptionalPropertyTypes being disabled in mind.

@ahejlsberg
Copy link
Member

We could consider requiring no line breaks between typeof xxx and a type argument list. Very simple fix, doesn't seem like an unreasonable rule.

@nicolo-ribaudo
Copy link

Note that requiring no line break is consistent with what TS already does in other cases (I was going to open an issue suggesting to require it to make ASI consistent, then I found this issue already opened!):

// valid, a type alias followed by a JSX element
type A = b
<c></c>
// this should be valid too
type A = typeof b
<c></c>

@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed Working as Intended The behavior described is the intended behavior; this is not a bug labels Apr 18, 2022
@RyanCavanaugh RyanCavanaugh removed the Breaking Change Would introduce errors in existing code label Apr 18, 2022
@typescript-bot typescript-bot added the Fix Available A PR has been opened for this issue label Apr 18, 2022
@jakebailey
Copy link
Member

I've sent #48755 which disallows the newline, unbreaking this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
8 participants