Skip to content

New keyword infer to intentionally identify a known inferable type #36450

@Shinigami92

Description

@Shinigami92

Search Terms

infer

Suggestion

Add the possibility to type a variable with const myValue: infer = complexThing;

Use Cases

This solves the problem of having to disable the line with eslint/tslint if the rule typedef is intentionally activated

Examples

(only one example)
I have created a very complex smart class that deeply infers column-names for a table.
The class member table is now a very huge type and therefore I dont want to type it.
But I also dont want to write table: any or table: unknown

Details

Bildschirmfoto 2020-01-27 um 13 27 58

@Component
export default class MyVueComponent {
  // eslint-disable-next-line @typescript-eslint/typedef
  public readonly table = new DataTable({
                    // ~~ dont want it to be any or unknown
    columnDefinitions: [
      {
        name: 'id',
        text: this.$t('table.id'),
        align: 'end',
        sortable: true,
        width: 120
      },
      {
        name: 'value',
        text: this.$t('table.value'),
        sortable: true,
        width: 250
      },
      {
        name: 'type',
        text: this.$t('table.type'),
        sortable: true,
        width: 150
      },
      {
        name: 'synonyms',
        text: this.$t('table.synonyms'),
        sortable: false
      },
      {
        name: 'link',
        text: this.$t('table.link'),
        sortable: true,
        width: 90,
        hideable: true,
        guard: () => this.$auth.isDeveloper()
      },
      {
        name: 'linkApproved',
        text: this.$t('table.link-approved'),
        sortable: true,
        width: 140,
        hideable: true,
        guard: () => this.$auth.isDeveloper()
      },
      {
        name: 'modifiedAt',
        text: this.$t('table.modified-at'),
        sortable: true,
        width: 150,
        hideable: true
      },
      {
        name: 'textAdUsage',
        text: this.$t('table.text-ad-usage'),
        sortable: true,
        width: 150,
        hideable: true
      },
      {
        name: 'group',
        text: this.$t('table.entity-group'),
        sortable: true,
        width: 140,
        hideable: true
      },
      {
        name: 'relation',
        text: this.$t('table.relation-to'),
        sortable: true,
        width: 170,
        hideable: true
      },
      {
        name: 'standalone',
        text: this.$t('table.standalone'),
        sortable: true,
        width: 180,
        hideable: true
      },
      {
        name: 'textAdAlternative',
        text: this.$t('table.text-ad-alternative'),
        sortable: true,
        width: 170,
        hideable: true
      },
      {
        name: 'actions',
        text: this.$t('global.table.header.actions'),
        width: 120
      }
    ]
  });
}

eslint blames me about to add a typedef to this line.

  1. I dont want to write the complete typedef to table and want the behaviour as not writing a typedef.
  2. I want to signalise other developers that this is fully intended and want the behaviour.
    So I can still profit of it when calling a function of it like this.table.setColumnVisibility('link', false);, were the first parameter is an union string literal of all possible hidden columns!

We cant create a type or interface with the name infer and use it as type definition
Therefore this will not be a breaking change!

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions