Comments (10)
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.
Duplicate of #55201.
from typescript.
but item.unit
type be unknown
,
Expected behavior
item.unit
type be string
,
from typescript.
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.
Objects are not sealed. It's possible that your
TypeA
values have aunit
property that is not astring
, that's why checking for a property using thein
operator results inunknown
.
So there is a problem with item's type recognition
But it was marked as non-defective in another issue
from typescript.
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.
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.
So there is a problem with item's type recognition
No, it's working as intended. There is no defect.
from typescript.
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.
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)
- Cannot read properties of undefined (reading 'charCount') HOT 1
- `tsBuildInfoFile` compiler option should be allowed without `incremental` or `composite` in Typescript versions `>5.6`
- super() typed as returning void HOT 2
- Move to File does not generate imports in the same way that autoimports does
- Move To File makes extra spaces when moving jsdoc comments
- (JSDoc, JavaScript) Imported class types by require() are unresolved HOT 1
- Type all exported members according to some interface HOT 2
- JSDoc typed function comments inside functions
- Error when accessing CSS property value using kebab case in `CSSStyleDeclaration` object HOT 1
- Type 'never' incorrectly inferred for mixed primitive type properties assignment with strictNullChecks disabled HOT 2
- Array of object unions not working as expected HOT 2
- Support WebIDL interfaces nominally (not just structurally) HOT 9
- Improve type inference for nullish coalescing (??) after null checks on multiple variables HOT 2
- [NewErrors] 5.7.0-dev.20240915 vs 5.6.2 HOT 6
- [ServerErrors][JavaScript] 5.7.0-dev.20240915 vs 5.6.2 HOT 3
- [ServerErrors][TypeScript] 5.7.0-dev.20240915 vs 5.6.2 HOT 4
- Inlay parameter hints don't account for trailing required parameters HOT 1
- JsDoc with overloads and different generics results in wrong dts output
- Generic Conditional Type resolved to `any` when passed an error `any` type HOT 8
- Typescript v5.6.2 resulting in "Overload signatures must all be optional or required." on alpine linux arm64 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.