typed-ember / glint

TypeScript powered tooling for Glimmer templates
https://typed-ember.gitbook.io/glint
MIT License
109 stars 51 forks source link

Narrowing `@namedArgs` does not work, but narrowing `this.args.namedArgs` does work. (@X named args are not aliased to "this.args.X" internally) #715

Open NullVoxPopuli opened 6 months ago

NullVoxPopuli commented 6 months ago

This correctly narrows:

function isMulti(x: 'single' | 'multi' | undefined): x is 'multi' {
  return x === 'multi';
}

export class ToggleGroup<Value = any> extends Component<{
  Element: HTMLDivElement;
  Args: SingleSignature<Value>['Args'] | MultiSignature<Value>['Args'];
  Blocks: {
    default: [
      {
        Item: Item;
      },
    ];
  };
}> {
  <template>
    {{#if (isMulti this.args.type)}}
      <MultiToggleGroup @value={{this.args.value}} @onChange={{this.args.onChange}} ...attributes />
    {{else}}
      <SingleToggleGroup @value={{this.args.value}} @onChange={{this.args.onChange}} ...attributes />
    {{/if}}
  </template>
}

This does not:

function isMulti(x: 'single' | 'multi' | undefined): x is 'multi' {
  return x === 'multi';
}

export class ToggleGroup<Value = any> extends Component<{
  Element: HTMLDivElement;
  Args: SingleSignature<Value>['Args'] | MultiSignature<Value>['Args'];
  Blocks: {
    default: [
      {
        Item: Item;
      },
    ];
  };
}> {
  <template>
    {{#if (isMulti @type)}}
      <MultiToggleGroup @value={{@value}} @onChange={{@onChange}} ...attributes />
    {{else}}
      <SingleToggleGroup @value={{@value}} @onChange={{@onChange}} ...attributes />
    {{/if}}
  </template>
}

these should be equiv, but @namedArg syntax should be all that folks use, as it's consistent cross-component authoring format. this.args in templates is discouraged and Glint should not encourage it :sweat_smile:

Here is a TSPlayground showing that what I want to do (and the this.args example) works in plain TS.

NullVoxPopuli commented 6 months ago

This triggers a failure for: https://github.com/ember-template-lint/ember-template-lint/blob/master/docs/rule/no-args-paths.md