Comments (12)
@typeholes Just to be clear: From the type checker's point of view there's no difference between the explicitly typed o
in your example and the explicitly typed i
in Martin's. Returning (string | null)[]
when Object.values
is called on an I
wouldn't be sound.
from typescript.
This is working as intended.
This is perfectly legal code, and a return type of (string | null)[]
would be wrong:
const o = { foo: 123, 5: 'test', 8: null };
const i: I = o;
from typescript.
See this FAQ entry: https://github.com/microsoft/TypeScript/wiki/FAQ#indirect-excess-properties-are-ok
from typescript.
Scratch that, I see the issue and it should really be returning unknown[]
I understand how that FAQ applies to Object.keys
, but I do not think it applies here.
const o = { foo: 123, 5: 'test', 8: null };
const i: I = o;
Here you have explicitly type i: I
so I would expect that Object.values
would return I[string|number][]
(edit: per fatCerebus's correction)
note that if you let i
be inferred the result would be (string | number | null)[]
which is also correct
from typescript.
Why would Object.values(i)
return I[]
? None of the properties are I
s. The point Martin is making is that TS is only seeing the types here and there's no guarantee that a thing typed as I
only has string | null
values.
from typescript.
Scratch that, I see the issue and it should really be returning
unknown[]
Agreed, but that would be a very big breaking change for little gain.
Here you have explicitly type
i:
That was intentional to demonstrate the point. You don't know where and how the object was created.
from typescript.
Interestingly we have the exact same issue with a record, but do get the specific result type
type T = Record<number, string|null>
interface I { [x: number]: string | null ; }
const o = { foo: 123, 5: 'test', 8: null };
const oT: T = o; const ovT = Object.values(oT); // (string | null)[]
const oI: I = o; const ovI = Object.values(oI); // any[]
from typescript.
Object.values has an overload for array-likes and string index signatures (but not bare numeric index signatures).
values<T>(o: { [s: string]: T; } | ArrayLike<T>): T[];
Supporting arrays makes sense. I'm guessing the string index signature is meant for object-as-a-dictionary scenarios. Either one has a potential hazard with excess properties, though.
from typescript.
Correct, so my question is should we be inconsistent between the interface and record values. I think consistency would be better, especially when you consider that any
is just explicitly unsound.
from typescript.
It's not really inconsistent though. Index signatures and interfaces represent different things.
Index signatures are "arrayish." They may not necessarily have numeric keys, but they represent collections of key/value pairs where the "element" values all conform to a single type, so it makes sense for Object.values
to support this scenario.
By its very nature, an interface is not exhaustive. An interface represents a subset of an object's actual shape (or a commonality among otherwise differently-shaped objects). So even if Object.values
returned a union of known property types based on the interface, you would still have to include unknown
in the union to account for properties not identified by the interface. And if you include unknown
in the union, then the entire thing reduces to unknown
.
That it returns any
instead of unknown
is unfortunate, but it's due to historical reasons.
from typescript.
Implementation is in #58358
from typescript.
This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.
from typescript.
Related Issues (20)
- tsc --init update 2024 HOT 5
- [transpileDeclaration API][5.5] Type containing enum values is incorrectly emitted HOT 3
- 5.5 Beta - Error assigning const string to enum from another file HOT 3
- `export default` generates JavaScript and declaration file incompatible HOT 3
- Issues with `isolatedDeclarations` and the associated fixes in editor HOT 5
- Incremental Builds are not Reliable v5.4.5 HOT 2
- Design Meeting Notes, 5/3/2024 HOT 1
- [transpileDeclaration API][5.5] Type-only import is missing when type is used as arrow function argument HOT 3
- Error on types when using multiple conditional properties HOT 3
- Compiler Option to monitor external dependencies
- Extract interface metadata for runtime HOT 2
- Enum allows numeric keys with leading 0s HOT 5
- Various bugs with class expressions and JavaScript decorators (code run twice, internal compiler crash) HOT 1
- JSDoc unexpected type error when spreading array containing different shapes of same type HOT 3
- Selecting suggestion of string completion deletes everything up to end of line, if no ending quote is present
- Hitting TS Limits: "Max Call Stack Size Exceeded" HOT 3
- [NewErrors] 5.5.0-dev.20240505 vs 5.4.5 HOT 52
- Error in Index Signature Assignment HOT 3
- [ServerErrors][TypeScript] 5.5.0-dev.20240505 HOT 15
- [ServerErrors][JavaScript] 5.5.0-dev.20240505 HOT 13
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.