Comments (12)
See https://github.com/microsoft/TypeScript/wiki/FAQ#indirect-excess-properties-are-ok
from typescript.
Indirect excess properties are not an error. Examples of indirect excess properties would be:
- values returned by functions
- values stored in intermediate variables
b
is direct. You go straight from value→property.
c
in indirect because it is one step removed. You have a getter, which is a function that returns a value.
From the linked FAQ, excess property checks are to defend against things like typos:
const p: Dimensions = {
width: 32,
height: 14,
depht: 11 // <-- typo!!
}
But there are plenty of situations where you want or need to allow extra properties.
from typescript.
You are conflating two different things. The autocomplete thing is unrelated to the type checking of c.data
.
b.data
andc.data
should throw errors simultaneously or not
No, they shouldn't; it's already been explained several times why c.data
doesn't have an error. The excess property check doesn't apply to it, but it's still type-checked properly. For example if you change the code to
const c: T = {
get data() {
return 42;
},
};
then you get a type error.
from typescript.
You are conflating two different things. The autocomplete thing is unrelated to the type checking of
c.data
.
b.data
andc.data
should throw errors simultaneously or notNo, they shouldn't; it's already been explained several times why
c.data
doesn't have an error. The excess property check doesn't apply to it, but it's still type-checked properly. For example if you change the code toconst c: T = { get data() { return 42; }, };then you get a type error.
Thank you, I understand my mistake
The real problem should be automatic completion
The problem of automatic completion made me think that c.data
had changed to any
type, resulting in the loss of type checking
from typescript.
See https://github.com/microsoft/TypeScript/wiki/FAQ#indirect-excess-properties-are-ok
bug b.data.b
throw type error. and in c.data
no type prompt like any
,
see typescirpt palay
from typescript.
Sigh... I really wish the wording of this error message would be changed.
This isn't a bug. The excess property check error is not a type error, it's a lint check. You can't rely on it to prevent extra properties from being put on an object because it won't always trigger (which is by design). If that's what you need, see #12936.
The other issue you linked to this (#49511) is unrelated - there is nothing like an any
involved here. { a: number, b: number }
is a legal subtype of and assignable to { a: number }
.
from typescript.
When inspecting the code I do find something looks like a bug: #58484.
@lqzhgood , is it what you are talking about?
from typescript.
Sigh... I really wish the wording of this error message would be changed.
This isn't a bug. The excess property check error is not a type error, it's a lint check. You can't rely on it to prevent extra properties from being put on an object because it won't always trigger (which is by design). If that's what you need, see #12936.
The other issue you linked to this (#49511) is unrelated - there is nothing like an
any
involved here.{ a: number, b: number }
is a legal subtype of and assignable to{ a: number }
.
The focus of this issue is not on whether additional attributes can be added,
Is difference types of b.data
and c.data
by use getters
b.data
and c.data
should throw errors simultaneously or not
from typescript.
When inspecting the code I do find something looks like a bug: #58484. @lqzhgood , is it what you are talking about?
yes,
d.data
and c.data
(getter) Inconsistent types returned,(from the perspective of throwing exceptions)
and c.data
looks like any
in autocomplete
from typescript.
Indirect excess properties are not an error. Examples of indirect excess properties would be:
- values returned by functions
- values stored in intermediate variables
b
is direct. You go straight from value→property.
c
in indirect because it is one step removed. You have a getter, which is a function that returns a value.From the linked FAQ, excess property checks are to defend against things like typos:
const p: Dimensions = { width: 32, height: 14, depht: 11 // <-- typo!! }But there are plenty of situations where you want or need to allow extra properties.
Thank you for your answer, but c.data
look like any
in autocomplete.
from typescript.
In that case, this is a retroactive duplicate of #58484 😄
from typescript.
the real problem is #58484. I will close this issue
from typescript.
Related Issues (20)
- Duplicate exports.* = assignments in CommonJS output in some cases
- [Wiki] Breaking changes. TypeScript 3.9. Exports Now Use Getters for Live Bindings.
- [NewErrors] 5.6.0-dev.20240604 vs 5.4.5 HOT 69
- Type Inference in Array callback is confusing HOT 1
- Autocomplete is not reliable with subpath exports HOT 2
- VS Code TS plugin Quick Fix Expansion Incorrect
- Cannot `infer` the return type of a generic function in a conditional type HOT 1
- Types of parameters 'property' and 'property' are incompatible. Type 'string | number | symbol' is not assignable to type 'keyof NamingDetail'.
- [compiler crash] TypeError: Cannot read properties of undefined (reading 'kind')
- Newlines removed/incorrect code generated for `getPasteEdits` HOT 3
- Intellisense stops working after exporting interface HOT 1
- Object is possibly 'null' when using type guard for for loops HOT 2
- Feature request: Override and extend callback argument
- Getters in mixins get serialized to property accessors HOT 1
- TS incorrectly flags a side effect-only import as TS5097 (An import path can only end with a '.ts' extension...) in `--watch` mode HOT 6
- Add typings for Iterator instance methods: take, find, map, etc. HOT 2
- instanceof has been used and there is a type inference error HOT 3
- False positive ts(2373)
- Type narrowing not work HOT 8
- `Uint8Array.prototype.constructor` is missing/incorrect type, and probably much more HOT 8
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.