Closed hckhanh closed 3 years ago
We don't have any mechanisms in place that can account for the complementarity of x
and !x
and don't plan to add any. There's no reason not to write this code more straightforwardly:
if (data) {
return data.array;
}
if (error) {
return "error";
}
return "loading";
have you attempted your band-aid work around on a live THIS object?
this-issue is terrible
its wiped out my pre handler hook I cannot even reference THIS on any of these lines and its blocking all attempts to augment any reference to THIS fail
@pre<Question>('save', (next) => {
if (this.createdAt !== null) {
this.createdAt = this.updatedAt = new Date()
}
this.updatedAt = new Date()
next()
})
export class Question extends Typegoose implements IQuestion {
I have a previous released project using a lesser version of typescript 4.0.5 that works fine with above code
im on the typescript 4.1.3 and the above code refuses to work
I dont want to sound offensive typescript has held its own...awesome work... but this is feeling like a nasty regression
I have set all meta flags in tsconfig as appropriate
"strict": false,
"strictFunctionTypes": false,
"noStrictGenericChecks": false,
"sourceMap": true,
"declaration": false,
"module": "ESNext",
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
@nhhockeyplayer can you please log a new issue with a complete repro? This doesn't look related to the problem described in the OP
Ok I will check my fire
I backed down to 4.0.5 and its happening on both
I have alot of code spread out across several projects
dont be alarmed if its legit you will hear more from me
Im still in shakedown mode
I understand that there's no way to detect what the code does, but how can I change this code to make ts not throw this error?
interface exampleType {
property?: [number]
}
function example(someObject: exampleType) {
if (!('property' in someObject)) someObject.property = [0]
return someObject.property[0] // Error: Object is possibly undefined
}
example({})
https://codesandbox.io/s/error-ts2532-object-is-possibly-undefined-3c3lo?file=/src/index.ts
interface exampleType {
property?: [number]
}
function example(someObject: exampleType) {
someObject.property ??= [0]
return someObject.property[0]
}
example({})
this remains an issue
TS2532: Object is possibly 'undefined'.
@pre<RoleEntity>('save', (next) => {
if (!this.createdAt) {
this.createdAt = this.updatedAt = new Date()
}
this.updatedAt = new Date()
next()
})
export class ...
unable to operate THIS pointer
its not very encouraging to close something that was not even vetted to be closed
does integrity exist in this world?
why would someone do this?
just to close it and make it gone? Does that do truth anything? ??
Maybe try:
this.createdAt ??= this.updatedAt = new Date()
I think the problem is that arrow functions don't have a 'this' try using an old school function definition?
this remains an issue
TS2532: Object is possibly 'undefined'.
@pre<RoleEntity>('save', (next) => { if (!this.createdAt) { this.createdAt = this.updatedAt = new Date() } this.updatedAt = new Date() next() }) export class ...
unable to operate THIS pointer
@RyanCavanaugh
TypeScript Version: v4.2.0-dev.20201112
Search Terms: TS2532, undefined
Code
Expected behavior: There is no way
data
can beundefined
when it's renderedActual behavior: Object is possibly 'undefined'. (TS2532) appears at
data.array
Playground Link: https://codesandbox.io/s/object-is-possibly-undefined-ts2532-n3kxp
Related Issues: I already searched but no result for me.