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

if type errors #1181

Open
edemaine opened this issue Apr 25, 2024 · 0 comments
Open

if type errors #1181

edemaine opened this issue Apr 25, 2024 · 0 comments
Labels
bug Something isn't working typescript TypeScript compatibility or extensions

Comments

@edemaine
Copy link
Collaborator

edemaine commented Apr 25, 2024

I was trying to write a proper type for our compile function (once it becomes async):

export function compile<T extends CompileOptions>(src: string, options?: T): Promise<
  if T extends { ast: true }
    CivetAST
  else
    if T extends { sourceMap: true }
      code: string
      sourceMap: SourceMap
    else
      string
>

The current compilation has multiple bugs:

export function compile<T extends CompileOptions>(src: string, options?: T): Promise<
  (T extends { ast: true }?
    CivetAST
  :
    (T extends { sourceMap: true }?
      {code: string}:never)),
      {sourceMap: SourceMap},
    else,
      string
>

It's missing two close parentheses, and it says else, instead of the second :.

Also, it would be nice to be able to write else if here.

Related, it's not possible to have a newline after : in a ?: type:

type CompileOutput<T extends CompileOptions> =
  T extends { ast: true } ? CivetAST :
  T extends { sourceMap: true } ? {
    code: string,
    sourceMap: SourceMap,
  } : string
@edemaine edemaine added bug Something isn't working typescript TypeScript compatibility or extensions labels Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working typescript TypeScript compatibility or extensions
Projects
None yet
Development

No branches or pull requests

1 participant