microsoft / microsoft/TypeScript

Error when initialising variable with generic parameter type

Open
#53,620 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Bug Domain: Mapped Types Help Wanted
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Bug Report

I am seeing an error when trying to initialise a variable with

const result: Partial<T> & { bar: boolean } = { bar: false };

where T is a type parameter in a generic function. The error is as follows:

4:9 - error TS2322: Type '{ bar: false; }' is not assignable to type 'Partial<T> & { bar: boolean; }'.
  Type '{ bar: false; }' is not assignable to type 'Partial<T>'.

4   const result: Partial<T> & { bar: boolean } = { bar: false };
          ~~~~~~

If T is a type that is not a generic parameter then there are no issues, and also adding an additional step as follows leads to no errors:

  const part: Partial<T> = {};
  const result: Partial<T> & { bar: boolean } = { ...part, bar: false };

I have included a full code snippet below.

🔎 Search Terms
  • "is not assignable partial"
  • "intersection type generic function error"
🕗 Version & Regression Information
  • This is the behaviour in every version I tried, and I reviewed the FAQ for entries about 'Common "Bugs" That Aren't Bugs'.
⏯ Playground Link

Playground link with relevant code

💻 Code
type PartialWithBar<T> = Partial<T> & { bar: boolean };

export const erroringFunc = <T extends Record<string, unknown>>(): PartialWithBar<T> => {
  const result: PartialWithBar<T> = { bar: false };
  return result;
};

export const workingFunc = <T extends Record<string, unknown>>(): PartialWithBar<T> => {
  const part: Partial<T> = {};
  const result: PartialWithBar<T> = { ...part, bar: false };
  return result;
};

type MyType = { a: number };

export const workingFunc2 = (): PartialWithBar<MyType> => {
  const result: PartialWithBar<MyType> = { bar: false };
  return result;
};
🙁 Actual behavior

erroringFunc causes an error, while workingFunc does not.

🙂 Expected behavior

Either errorringFunc should not cause any errors, or workingFunc should cause an error - the two should be consistent.

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

Start with the linked TypeScript Playground and compare the type-checking behavior of erroringFunc, workingFunc, and workingFunc2 in the provided snippet. Investigate why the object literal is rejected for PartialWithBar while the spread form is accepted; done means the two generic forms have consistent, tested behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.