microsoft / microsoft/TypeScript

Allow instantion expression with empty arguments

Open
#57,510 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔍 Search Terms

instantiation expression, empty, type arguments

✅ Viability Checklist
⭐ Suggestion

I would like to be able to instantiate a generic function type with an empty type arguments list.

It would be nice to allow this for generic types, but it's not as necessary, since a generic type is always instantiated in a type context, even if no type arguments list is given. A generic function type needs to be specifically instantiated.

📃 Motivating Example

Here is a generic function where a type parameter has a default. I can get use an instantiation expression to specialize it to a non-generic function.

declare function find1<T, S extends T=T>(Ar:T[], predicate:(t:T)=>t is S): S | undefined;
type Find1Object = typeof find1<object>;

Here is a generic function with only one type parameter. I cannot use an instantiation expression with the default for S:

declare function find2<S extends object = object>(Ar:object[], predicate:(t:object)=>t is S) : S | undefined;
type Find2 = typeof find2<>; // <- this is a syntax error "Type argument list cannot be empty"
💻 Use Cases
  1. What do you want to use this for?
  2. What shortcomings exist with current approaches?
  3. What workarounds are you using in the meantime?
    • Declare an unused "dummy" type parameter so there's always a non-defaulted type argument.

Originally mentioned here: https://github.com/microsoft/TypeScript/issues/57463#issuecomment-1957739330

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files or tests; start by locating the parser and type-checking paths for instantiation expressions and the diagnostic rejecting an empty type-argument list. Use the find2 example as the acceptance case: an empty list should be accepted and specialize the generic function using its defaults without changing emitted JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.