TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

No ts 2352 for object literal with missing properties

Open MansurAliKoroglu opened this issue 2 years ago • 3 comments

Bug Report

🔎 Search Terms

google search

site:github.com/microsoft/TypeScript/issues ("ts(2352)" OR "TS2352" OR "neither type sufficiently overlaps" OR "as unknown") ("instanceof" OR "typeof" OR "narrowing" OR "type guard") " as "

🕗 Version & Regression Information

  • Tested v5.0.4
  • Tested nightly

⏯ Playground Link

Playground link with relevant code

💻 Code

interface ITest {
  first: number;
  second: string;
}

// No error on this as ITest
const foo = {
  first: 2
} as ITest;

🙁 Actual behavior

No codefix.

🙂 Expected behavior

Codefix to convert to 0 as unknown as string. ts(2352)

MansurAliKoroglu avatar May 05 '23 04:05 MansurAliKoroglu

This is working as intended. You instruct the compiler to consider your incomplete object as a ITest using the type assertion. The types overlap, so no temporary as unknown step is necessary.

MartinJohns avatar May 05 '23 04:05 MartinJohns

When I change first's type to string. I started to get ts(2352) That's what confused me.

const foo = {
  first: 'string'
} as ITest;

This issue can be closed if works as intended.

MansurAliKoroglu avatar May 05 '23 07:05 MansurAliKoroglu

When you change it to string you don't have it overlapping anymore, there's no common property.

MartinJohns avatar May 05 '23 07:05 MartinJohns