Giter VIP home page Giter VIP logo

Comments (10)

MartinJohns avatar MartinJohns commented on September 27, 2024 1

As mentioned in the other issue, it's due to subtype reduction. Look it up, there are many issues about it. It's working as intended.

You can either explicitly check if unit is a string by doing 'unit' in item && typeof item.unit === 'string' or just use a type assertion item.unit as string. But again, objects are not sealed. Values of TypeA are permitted to have a unit property at runtime, which may not be typed string. This adjustment of your code demonstrates it:

interface TypeA {
  name: string;
}

interface TypeB {
  name: string;
  unit: string;
}
let unit: string; 
const testObject: { a: TypeB[], b: TypeA[] } = {
  a: [],
  b: [],
};


// The value is compatible with TypeA, so it can be pushed to the TypeA array.
const value = { name: 'example', unit: 12345 };
testObject.b.push(value);


(['a', 'b'] as const).forEach((key) => {
  const ObjectValue = testObject[key]

  ObjectValue.forEach((item) => {
    if('unit' in item) {
      console.log(item.unit)
      unit = item.unit // error ts(2322)
    }
  })
})

from typescript.

MartinJohns avatar MartinJohns commented on September 27, 2024

Duplicate of #55201.

from typescript.

chenyulun avatar chenyulun commented on September 27, 2024

@RyanCavanaugh

but item.unit type be unknown,

Expected behavior

item.unit type be string,

from typescript.

MartinJohns avatar MartinJohns commented on September 27, 2024

Objects are not sealed. It's possible that your TypeA values have a unit property that is not a string, that's why checking for a property using the in operator results in unknown.

from typescript.

chenyulun avatar chenyulun commented on September 27, 2024

Objects are not sealed. It's possible that your TypeA values have a unit property that is not a string, that's why checking for a property using the in operator results in unknown.

So there is a problem with item's type recognition
But it was marked as non-defective in another issue

from typescript.

chenyulun avatar chenyulun commented on September 27, 2024

How should I write best practices for the following code

export interface TypeA {
  name: string;
}

export interface TypeB {
  name: string;
  unit: string;
}
let unit: string; 
const testObject: { a: TypeB[], b: TypeA[] } = {
  a: [],
  b: [],
};
(['a', 'b'] as const).forEach((key) => {
  const ObjectValue = testObject[key]

  ObjectValue.forEach((item) => {
    if('unit' in item) {
      unit = item.unit // error ts(2322)
    }
  })
})

from typescript.

chenyulun avatar chenyulun commented on September 27, 2024
export interface TypeA {
  name: string;
  // age: number;
}

export interface TypeB {
  name: string;
  unit: string;
}
type TypeTest = TypeA[] | TypeB[]
// type TypeTest = string[] | boolean[];




type Item = TypeTest extends Array<infer R> ? R : never // as  type Item = TypeA
// because this?
type IsBExtendsA = TypeB extends TypeA ? 'true' : 'false' // 'true'

from typescript.

MartinJohns avatar MartinJohns commented on September 27, 2024

So there is a problem with item's type recognition

No, it's working as intended. There is no defect.

from typescript.

chenyulun avatar chenyulun commented on September 27, 2024
export interface TypeA {
  name: string;
  // age: number;
}

export interface TypeB {
  name: string;
  unit: string;
}
type TypeTest = TypeA[] | TypeB[]
// type TypeTest = string[] | boolean[];




type Item = TypeTest extends Array<infer R> ? R : never // as  type Item = TypeA
// because this?
type IsBExtendsA = TypeB extends TypeA ? 'true' : 'false' // 'true'

Although the logic is fine, the extends judgment in real use scenarios doesn't make sense, and this may involve too much underlying design.

from typescript.

chenyulun avatar chenyulun commented on September 27, 2024

So there is a problem with item's type recognition

No, it's working as intended. There is no defect.

But he realistically lost the type of unit as string,Is there a better way to practice this situation?

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.