0

In TypeScript on VS Code I have the following type:

export type PgInheritableTableWithColumns<
  T extends TableConfig
> = PgTableWithColumns<T> & {
  extend<
    TNewTableName extends string,
    TNewColumnsMap extends Record<string, PgColumnBuilderBase>
  > (
    name: TNewTableName,
    columns: TNewColumnsMap,
    extraConfig?: (self: BuildExtraConfigColumns<TNewTableName, TNewColumnsMap, 'pg'>) => PgTableExtraConfigValue[]
  ): PgInheritableTableWithColumns<{
    name: TNewTableName;
    schema: T['schema'];
    columns: BuildColumns<TNewTableName, TNewColumnsMap, 'pg'> & T['columns'];
    dialect: 'pg';
  }>;

  extend<
    TNewTableName extends string,
    TNewColumnsMap extends Record<string, PgColumnBuilderBase>
  >(
    name: TNewTableName,
    columns: (columnTypes: PgColumnsBuilders) => TNewColumnsMap,
    extraConfig?: (self: BuildExtraConfigColumns<TNewTableName, TNewColumnsMap, 'pg'>) => PgTableExtraConfigValue[]
  ): PgInheritableTableWithColumns<{
    name: TNewTableName;
    schema: T['schema'];
    columns: BuildColumns<TNewTableName, TNewColumnsMap, 'pg'> & T['columns'];
    dialect: 'pg';
  }>;
};

Why does it mess up my syntax highlighting in the rest of the file?

Messed up syntax highlighting:

Messed up syntax

Correct highlighting:

Correct highlighting

By splitting up the type into type + interface like

interface ExtendFn<T extends TableConfig> {
  extend<
    TNewTableName extends string,
    TNewColumnsMap extends Record<string, PgColumnBuilderBase>
  >(
    name: TNewTableName,
    columns: TNewColumnsMap,
    extraConfig?: (self: BuildExtraConfigColumns<TNewTableName, TNewColumnsMap, 'pg'>) => PgTableExtraConfigValue[]
  ): PgInheritableTableWithColumns<{
    name: TNewTableName;
    schema: T['schema'];
    columns: BuildColumns<TNewTableName, TNewColumnsMap, 'pg'> & T['columns'];
    dialect: 'pg';
  }>;

  extend<
    TNewTableName extends string,
    TNewColumnsMap extends Record<string, PgColumnBuilderBase>
  >(
    name: TNewTableName,
    columns: (columnTypes: PgColumnsBuilders) => TNewColumnsMap,
    extraConfig?: (self: BuildExtraConfigColumns<TNewTableName, TNewColumnsMap, 'pg'>) => PgTableExtraConfigValue[]
  ): PgInheritableTableWithColumns<{
    name: TNewTableName;
    schema: T['schema'];
    columns: BuildColumns<TNewTableName, TNewColumnsMap, 'pg'> & T['columns'];
    dialect: 'pg';
  }>;
}

export type PgInheritableTableWithColumns<
  T extends TableConfig
> = PgTableWithColumns<T> & ExtendFn<T>;

The highlighting goes back to normal. Is it a VS Code bug?

1
  • I don't know why (it looks related to the intersection type, but also does not work with a union type), but if you put extends up to and including the opening parentheses on a single line, the syntax highlighting seems to work. Both: extend<TNewTableName extends string, TNewColumnsMap extends Record<string, PgColumnBuilderBase>>( Commented Nov 13 at 17:03

2 Answers 2

1

It seems to be an issue with the TextMate grammar file for TypeScript for VS Code.

It doesn't properly support the extends keyword, here is an open issue portraying the same behavior https://github.com/microsoft/TypeScript-TmLanguage/issues/1048

The workaround you found seems to be working

New contributor
Soufiane Sakhi is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Sign up to request clarification or add additional context in comments.

Comments

0

caused by the TypeScipt TextMate grammar not supporting multi-line function typeparameters correctly

heres the repo https://github.com/microsoft/TypeScript-TmLanguage/issues
and a similar issue https://github.com/microsoft/TypeScript-TmLanguage/issues/1048

single line works correctly

type ABC = string;

export type Foo =  {
  extend<ABC extends string,> (): string;
};

export const A = 5;

multi-line broken

type ABC = string;

export type Foo =  {
  extend<
    ABC extends string,
  > (): string;
};

export const A = 5;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.