question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug] TemplateStringsArray is incompatible with literal array type

See original GitHub issue

TypeScript Version: 2.4.0

Code

// A *self-contained* demonstration of the problem follows...
interface SQLQuery<TResult> {
  __result: TResult;
} 
declare function sql(
  literals: ['SELECT id FROM users'],
  ...placeholders: any[]
): SQLQuery<{id: number}>;

declare function querySync<TResult>(q: SQLQuery<TResult>): Array<TResult>;

const values: Array<{id: number}> = querySync(sql`SELECT id FROM users`);

Expected behavior:

Typescript should see that the input string ‘SELECT id FROM users’ matches the expected literals of ['SELECT id FROM users'] and use the declared function, allowing me to generate an overloaded version of the sql function for each query.

Actual behavior:

I get the error:

src/index.ts(23,50): error TS2345: Argument of type ‘TemplateStringsArray’ is not assignable to parameter of type ‘[“SELECT id FROM users”]’. Property ‘0’ is missing in type ‘TemplateStringsArray’.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:3
  • Comments:7 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
RyanCavanaughcommented, Aug 15, 2018

@DanielRosenwasser can you give an update on next steps here?

1reaction
pablobirukovcommented, Jun 21, 2017
declare function id<T extends string>(list: Array<T>): Array<T>;
declare function readonlyId<T extends string>(list: ReadonlyArray<T>): ReadonlyArray<T>;

type LiteralType = "foo" | "bar";

const array: Array<LiteralType> = [];
const readonlyArray: ReadonlyArray<LiteralType> = [];

const foo: ReadonlyArray<LiteralType> = id(array);
const bar: ReadonlyArray<LiteralType> = readonlyId(array);
//    ~~~
// Type 'ReadonlyArray<string>' is not assignable to type 'ReadonlyArray<LiteralType>'.
//  Type 'string' is not assignable to type 'LiteralType'.const bar: ReadonlyArray<LiteralType>
const baz: ReadonlyArray<LiteralType> = readonlyId(readonlyArray);

Isn’t it a bug that generic type information is being lost when ReadonlyArray is assigned to Array?

Read more comments on GitHub >

github_iconTop Results From Across the Web

flowtype cannot return object literal because object type is ...
When I run flow over this function, I get the following error: Cannot return object literal because object type [1] is incompatible with ......
Read more >
Template literals (Template strings) - JavaScript | MDN
Template literals are literals delimited with backtick (`) characters, allowing for multi-line strings, string interpolation with embedded ...
Read more >
Documentation - Template Literal Types - TypeScript
Generating mapping types which change properties via template literal strings.
Read more >
Overview - TypeScript
can be rewritten as the following array literal ... union member and have the appropriate type, meaning that the sample above correctly issues...
Read more >
Arrays - Flow
Array types are simply instantiations of a special polymorphic Array ... Flow would report an error for an incompatible element write a[i] =...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found