Repository navigation
[bug] TemplateStringsArray is incompatible with literal array type #16552
Description
Activity
- addedBugA bug in TypeScriptA bug in TypeScript
on Jun 16, 2017 DanielRosenwasser commented
on Jun 16, 2017 MemberMore actionsI think this is contingent on #16592.
- addedSuggestionAn idea for TypeScriptAn idea for TypeScriptand removedBugA bug in TypeScriptA bug in TypeScript
on Jun 16, 2017 DanielRosenwasser commented
on Jun 16, 2017 MemberMore actionsActually, I don't know if this will work even with the changes I have in mind.
TemplateStringsArrayis effectively aReadonlyArray<string>- but you can't assign aReadonlyArrayto a mutableArray. So at the minimum you'd have to write something likeinterface MyCustomType extends TemplateStringsArray { 0: "SELECT id FROM users" }
Reacted by jEnbuska and Axel RauschmayerForbesLindesay commented
on Jun 19, 2017 ContributorAuthorMore actionsThat would be fine. I'm going to be auto-generating these definitions anyway, by parsing the code to see which functions are called. This would be very much a workaround for #16551.
At the moment, that doesn't seem to work either though.
Reacted by jEnbuskadeclare 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
ReadonlyArrayis assigned toArray?Reacted by SlurpTheoForbesLindesay commented
on Jun 23, 2017 ContributorAuthorMore actionsRonny Friman (@R00GER) that looks like a separate bug, it should probably have its own issue.
RyanCavanaugh commented
on Aug 15, 2018 MemberMore actionsDaniel Rosenwasser (@DanielRosenwasser) can you give an update on next steps here?
Reacted by Johannes Schickling- addedIn DiscussionNot yet reached consensusNot yet reached consensus
on Nov 6, 2018 There shouldn't be a type
TemplateStringsArray, it should be aconst string tuple type, so we can write code:interface SQL<TSA, VS> { texts: TSA; values: VS; } function sql<TSA extends readonly string[], VS extends any[]>(texts:TSA, ...values: VS): SQL<TSA, VS> { return { texts, values }; } // then let s: SQL<['select * from person where a=', ' and b=', ''], [number, Date]> = sql`select * from person where a=${1} and b=${new Date()}`;
TypeScript Version: 2.4.0
Code
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 thesqlfunction for each query.Actual behavior:
I get the error: