Repository: TypeScript
Issue: Allow Type Checking to be Extended (into Tagged Templates, for example)
Labels: Suggestion,Awaiting More Feedback

## Search Terms

Tagged Templates, template literals, JSX, htm, lit, lit-html, hyperhtml, nanohtml, choo

## Suggestion

Many developers are exploring Tagged Templates as an alternative to JSX/TSX, as the result offers some important advantages. In particular, Tagged Templates parse faster than compiled JSX in all modern JS engines [1], and expose potential optimizations for consuming rendering libraries to be able to bypass comparison logic for identified static portions of a UI representation.

Regardless of the merit of these advantages, one of the key drawbacks cited by developers when adopting Tagged Templates in place of TSX is the lack of typing within static templates and associated expression parts.  This affects all libraries using Tagged Templates.

> [1]: analysis and benchmark data forthcoming, email me for access if necessary.

Using [htm](https://github.com/developit/htm) as an example:

```js
interface Props {
    sticky: boolean;
}
function Header({ sticky }: Props) {
  return html`<header class=${'header' + (sticky ? ' sticky' : '')} />`;
}
render(html`<${Header} sticky=${'yes'} />`);
// Incorrect type, but no error ^^^^^
```

([view example in playground](http://www.typescriptlang.org/play/#src=interface%20VNode%3CP%20%3D%20any%3E%20%7B%0D%0A%20%20%20%20tag%3A%20string%20%7C%20Component%3CP%3E%3B%0D%0A%20%20%20%20props%3A%20P%3B%0D%0A%20%20%20%20children%3A%20any%5B%5D%0D%0A%7D%0D%0A%0D%0Ainterface%20Component%3CP%20%3D%20%7B%7D%3E%20%7B%0D%0A%20%20%20%20(props%3A%20P)%3A%20VNode%3Cany%3E%20%7C%20null%3B%0D%0A%7D%0D%0A%0D%0Afunction%20render(html%3A%20string)%20%7B%0D%0A%20%20%20%20return%20html%3B%0D%0A%7D%0D%0A%0D%0Afunction%20h%3CP%3E(tag%3A%20string%20%7C%20Component%3CP%3E%2C%20props%3A%20%7B%7D%20%26%20P%20%7C%20null%2C%20...children%3A%20any)%3A%20VNode%20%7B%0D%0A%20%20%20%20return%20%7B%20tag%2C%20props%2C%20children%20%7D%3B%0D%0A%7D%0D%0A%0D%0A%2F%2F%20(%3C%3Esnip%3A%20parser%20for%20tagged%20template%20DSL)%0D%0Aconst%20html%20%3D%20(statics%3A%20TemplateStringsArray%2C%20...expr%3A%20any)%20%3D%3E%20statics.reduce((s%2C%20p%2C%20i)%20%3D%3E%20s%20%2B%20p%20%2B%20(i%20%3C%20expr.length%20%3F%20expr%5Bi%20%2B%201%5D%20%3A%20'')%2C%20'')%3B%0D%0A%0D%0Ainterface%20Props%20%7B%0D%0A%20%20%20%20sticky%3A%20boolean%3B%0D%0A%7D%0D%0Afunction%20Header(%7B%20sticky%20%7D%3A%20Props)%20%7B%0D%0A%20%20return%20html%60%3Cheader%20class%3D%24%7B'header'%20%2B%20(sticky%20%3F%20'%20sticky'%20%3A%20'')%7D%20%2F%3E%60%3B%0D%0A%7D%0D%0Arender(html%60%3C%24%7BHeader%7D%20sticky%3D%24%7B'yes'%7D%20%2F%3E%60)%3B%0D%0A))

Since the template's static parts are an unknown format, this is logical. However, consider the compiled output of the above:

```js
interface Props {
    sticky: boolean;
}
function Header({ sticky }: Props) {
  return h('header', {class: 'header' + (sticky ? ' sticky' : '')});
}
render(h(Header, { sticky: 'yes' }));
//                 ^^^^^^
//                 Type 'string' is not assignable to type 'boolean'.
```

([view example in playground](http://www.typescriptlang.org/play/#src=interface%20VNode<P%20%3D%20any>%20%7B%0D%0A%20%20%20%20tag%3A%20string%20%7C%20Component<P>%3B%0D%0A%20%20%20%20props%3A%20P%3B%0D%0A%20%20%20%20children%3A%20any%5B%5D%0D%0A%7D%0D%0A%0D%0Ainterface%20Component<P%20%3D%20%7B%7D>%20%7B%0D%0A%20%20%20%20(props%3A%20P)%3A%20VNode<any>%20%7C%20null%3B%0D%0A%7D%0D%0A%0D%0Afunction%20render(%7B%20tag%2C%20props%2C%20children%20%7D%3A%20VNode)%20%7B%0D%0A%20%20%20%20return%20%60<%24%7Btag%7D%24%7BObject.keys(props).reduce((s%2Ci)%3D>s%2Bi%2B'%3D"'%2Bprops%5Bi%5D%2B'"'%2C'')%7D>%24%7Bchildren.join('')%7D<%2F%24%7Btag%7D>%60%3B%0D%0A%7D%0D%0A%0D%0Afunction%20h<P>(tag%3A%20string%20%7C%20Component<P>%2C%20props%3A%20%7B%7D%20%26%20P%20%7C%20null%2C%20...children%3A%20any)%3A%20VNode%20%7B%0D%0A%20%20%20%20return%20%7B%20tag%2C%20props%2C%20children%20%7D%3B%0D%0A%7D%0D%0A%0D%0Ainterface%20Props%20%7B%0D%0A%20%20%20%20sticky%3A%20boolean%3B%0D%0A%7D%0D%0Afunction%20Header(%7B%20sticky%20%7D%3A%20Props)%20%7B%0D%0A%20%20return%20h('header'%2C%20%7Bclass%3A%20'header'%20%2B%20(sticky%20%3F%20'%20sticky'%20%3A%20'')%7D)%3B%0D%0A%7D%0D%0A%2F%2F%20Clearly%20a%20TypeError%0D%0Arender(h(Header%2C%20%7B%20sticky%3A%20'yes'%20%7D))%3B%0D%0A))

I would like to suggest that we need a solution for type-checking Tagged Templates. The shortest-path solution would be to special-case checking for Tagged Templates with a tag function that has a local identifier with the name `html`, though clearly that's not optimal as implementation of `html` can vary.

## Use Cases

The use-case for this is to allow developers to express view hierarchies in standard JavaScript syntax rather than JSX/TSX, while preserving the typing support currently offered by TSX being integrated into TypeScript's parser.

If a design were to be proposed to extend type checking to arbitrary opaque Tagged Templates (perhaps through plugins), this would allow a whole host of untyped code to be checked by the TypeScript Compiler. I'm fairly certain the various CSS-in-JS solutions would be also interested in this level of static analysis in order to errors currently handled at runtime into compilation.

## Examples

```js
interface Props {
    sticky: boolean;
}
function Header({ sticky }: Props) {
  return html`<header class=${'header' + (sticky ? ' sticky' : '')} />`;
}
render(html`<${Header} sticky=${'yes'} />`);
//                            ^^^^^^^^
//                            Type 'string' is not assignable to type 'boolean'.
```

## Checklist

My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals). (specifically goals 1, 2 & 6)

/cc @IgorMinar @JustinFagnani @RobWormald @Surma