microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.69k stars 12.44k forks source link

Design Meeting Notes, 8/18/2021 #45504

Closed DanielRosenwasser closed 2 years ago

DanielRosenwasser commented 3 years ago

Consistently Filtering Out Negative Matches when Narrowing

https://github.com/microsoft/TypeScript/pull/45205 https://github.com/microsoft/TypeScript/pull/41821

Playground Samples

Tagged Template Inferrence and Making TemplateStringsArray

https://github.com/microsoft/TypeScript/issues/31422 https://github.com/microsoft/TypeScript/issues/33304 https://github.com/microsoft/TypeScript/pull/45310

Notes from @RyanCavanaugh

declare function tag<T extends TemplateStringsArray>(strs: T): T;

let tsa: TemplateStringsArray;
let x = tag `hello world`;
DanielRosenwasser commented 3 years ago

To add some thoughts to the tagged template examples:

trusktr commented 2 years ago
  • Use cases here aren't super compelling, but feels like they could be?
    • Let's see more

Just the use case of

/** @type {HTMLDivElement} */
const div = html`<div>
  <${SomeComponent} fooProp=${123 /*fooProp accepts numbers only*/} />
  <${SomeComponent} fooProp=${"bar" /*type error*/} />
</div>`

is compelling enough. This means people can write type-safe DOM applications (or other applications in places similar to React native that render a declarative tree to native UI components), including type safety within the template string markup, without requiring a build step to transform JSX. For example, that code snippet could be pasted from VS Code (where the user would be able to catch errors in the html template) to CodePen and it would just work.

trusktr commented 2 years ago

This is a moderate 👎 - we think we'd be encouraging people to use the wrong tools to solve these problems

I don't think that using a build-less setup is the wrong tool in every case. There are very valid reasons to use no build tools:

Furthermore, build steps can optionally compile template tag strings to optimized formats just like with JSX, with the benefit that code can be written one way and it would work in both build-less or build-full setups; optimization when needed. That, with added type safety, is a nice win.

tobiasdiez commented 2 years ago
  • "I want to write a full GraphQL parser and provide exact API shapes back"

  • This is a moderate 👎 - we think we'd be encouraging people to use the wrong tools to solve these problems

Why are type-safe tagged template strings the wrong tool for declaring Graphql queries and what would be a better solution? The graphql codgen https://www.graphql-code-generator.com/plugins/gql-tag-operations-preset provides something similar but has to use the somewhat awkward convention gql(/* GraphQL */ query) due to the limitations of TS support for tagged templates.

ExE-Boss commented 2 years ago
  • "I want to write a full HTML parser and provide the same checking that JSX gives"
  • "I want to write a full GraphQL parser and provide exact API shapes back"
  • This is a moderate 👎 - we think we'd be encouraging people to use the wrong tools to solve these problems

Well, a full parser would probably be infeasible anyway due to type instantiation and recursion limits, but a simplified parser that correctly infers the return type of the html`<div>${text}</div>`⁠ expression as HTMLDivElement would be useful.

imjamesb commented 2 years ago

Please revisit generic type for template literals.