Typescript没有抱怨关于对象展开语法的类型错误。

huangapple go评论63阅读模式
英文:

Typescript is NOT complaining about type error in Object Spread Syntax

问题

以下是翻译好的部分:

以下片段应该引发 TypeScript 错误,但是在使用对象扩展语法时,任何片段都没有抱怨。

请注意,我故意为了这个测试在 address 属性上加了一个拼写错误。

片段 A.1. - 没有引发错误

interface TestType {
  company?: string;
  address?: string;
}

function testFunction(): TestType {
  const cond = true;
  const testDto = {
    ...(cond ? { company: '' } : {}),
    ...(cond ? { addressss: '' } : {}),
  };
  return testDto;
}

片段 A.2. - 没有引发错误

interface TestType {
  company?: string;
  address?: string;
}

function testFunction() {
  const cond = true;
  const testDto: TestType = {
    ...(cond ? { company: '' } : {}),
    ...(cond ? { addressss: '' } : {}),
  };
  return testDto;
}

片段 A.3. - 没有引发错误

interface TestType {
  company?: string;
  address?: string;
}

function testFunction() {
  const cond = true;
  const testDto= {
    ...(cond ? { company: '' } : {}),
    ...(cond ? { addressss: '' } : {}),
  };
  return testDto as TestType;
}

片段 B.1. - 成功引发错误

这个片段成功引发 TypeScript 错误,因为我移除了对象扩展语法,并且将接口定义放在了常量名后面。

interface TestType {
  company?: string;
  address?: string;
}

function testFunction() {
  const cond = true;
  const testDto: TestType = {
    company: '',
    addressss: '',
  };
  return testDto;
}

片段 B.2. - 没有引发错误

即使移除了对象扩展语法,但是接口定义放在函数返回类型中时,这个片段仍然没有引发 TypeScript 错误。

interface TestType {
  company?: string;
  address?: string;
}

function testFunction(): TestType {
  const cond = true;
  const testDto = {
    company: '',
    addressss: '',
  };
  return testDto;
}

我也在一个在线 TypeScript 编辑器中进行了测试,以排除可能与我的环境相关的问题。

有人可以帮我理解发生了什么吗?此外,有人能提供一个在使用对象扩展语法时检测拼写错误的解决方案示例吗?

非常感谢!

英文:

The following Snippets should throw a typescript error, but it's not complaining in any snippet when the object spread syntax is used.

Please note, that a added a typo for the address property on purpose for this test.

Snippet A.1. - not throwing error

interface TestType {
  company?: string;
  address?: string;
}

function testFunction(): TestType {
  const cond = true;
  const testDto = {
    ...(cond ? { company: '' } : {}),
    ...(cond ? { addressss: '' } : {}),
  };
  return testDto;
}

Snippet A.2. - not throwing error

interface TestType {
  company?: string;
  address?: string;
}

function testFunction() {
  const cond = true;
  const testDto:TestType = {
    ...(cond ? { company: '' } : {}),
    ...(cond ? { addressss: '' } : {}),
  };
  return testDto;
}

Snippet A.3. - not throwing error

interface TestType {
  company?: string;
  address?: string;
}

function testFunction() {
  const cond = true;
  const testDto= {
    ...(cond ? { company: '' } : {}),
    ...(cond ? { addressss: '' } : {}),
  };
  return testDto as TestType;
}

Snippet B.1. - successfully throwing error

This Snippet is successfully throwing a typescript error, because I removed the object spread syntax and put the interface definition right after the constant name.

interface TestType {
  company?: string;
  address?: string;
}

function testFunction() {
  const cond = true;
  const testDto:TestType = {
    company: '',
    addressss: '',
  };
  return testDto;
}

Snippet B.2. - not throwing error

This Snippet is still not throwing a typescript error, even if object spread syntax is removed, but the interface is defined in the function return type.

interface TestType {
  company?: string;
  address?: string;
}

function testFunction():TestType {
  const cond = true;
  const testDto = {
    company: '',
    addressss: '',
  };
  return testDto;
}

I also tested in an online typescript editor to exclude potential issues with my environment.

Can somebody help me understand what's going on? Also, could someone provide an example with a solution that detects misspellings when the object spread syntax is used?

Thanks a lot!

答案1

得分: 1

You declared company & address as optional (by using ?:). So TypeScript is okay with your return object not having those properties. This should throw an error:

interface TestType {
  company: string; // I removed the question mark
  address: string;
}

function testFunction(): TestType {
  const cond = true;
  const testDto = {
    ...(cond ? { company: '' } : {}),
    ...(cond ? { address: '' } : {}),
  };
  return testDto;
}
英文:

You declared company & address as optional (by using ?:).
So typescript is ok to that your return object hasn't those properties.
This should throw an error:

interface TestType {
  company: string; // I removed the questions mark
  address: string;
}

function testFunction(): TestType {
  const cond = true;
  const testDto = {
    ...(cond ? { company: '' } : {}),
    ...(cond ? { address: '' } : {}),
  };
  return testDto;
}

huangapple
  • 本文由 发表于 2023年5月10日 23:43:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76220367.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定