#is there a way in ts to just restric infer to key of the object ?

47 messages · Page 1 of 1 (latest)

keen iceBOT
#
jonlepage#0

Preview:```ts
type Data = {
//[k:string] : never
a:number
b:number
}

function foo1<T extends Data >(o:T) {}
function foo2<const T extends Data >(o:T) {}

foo1({a:8,b:4, gf:8}) // i want error gf is not in Data
foo2({a:8,b:4, gf:8}) // i want error gf is not in Data```

vestal oak
#

how can i force ts to give me error on the generic feeding when i use a key thats should not in the interface ?

feral kite
#

that's kinda the opposite of what generics are for

#

generics let you use more specific types, which you're doing there

vestal oak
#

I have a pattern which allows me to describe my systems and inject the generics on class (because constructor cant infer and i need duplicate code),
but with this approach , I can inject random fields which could be linked to a syntactic error
I just want to strengthen the detection to tell me hey you made a mistake in the descriptions of this system, the system will not work as expected !

feral kite
#

!screenshot

keen iceBOT
#
`!screenshot`:

Rather than screenshots, please provide either code formatted as:

```ts
// code here
```
Or even better, as an example on the TypeScript playground that is as simple as possible and reproduces the issue. This makes it easier to help you and increases the chances of getting an answer.

feral kite
#

because constructor cant infer
how so?

vestal oak
feral kite
#

that really doesn't tell me anything

vestal oak
feral kite
#

and it doesn't really make sense, because that's not what generics are for

#

this seems like a case of xyproblem, and i'm asking you about your actual problem

vestal oak
#

i see

feral kite
#

your screenshot doesn't tell me anything about your original problem about your constructor not being able to infer, which seems like it'd be the root of the problem and a fix should be targeted there

#

typescript itself also allows excess properties, just not in object literals.
if you want to have that behavior, then you probably don't want the parameter to be the full generic type, at the very least

vestal oak
feral kite
#

don't see any

#

oh, it just didn't load with the font, found it.

#

anyways, you didn't answer my question.

vestal oak
#

sorry tiny link because discord said message is too long

feral kite
#

what was your original problem about a constructor not inferring?

#

i gotta go sleep at this point, it's 1am for me.

vestal oak
feral kite
#

constructors can and will infer generics, what do you mean lmao

vestal oak
#

on my pattern i use concret to infer system (no interface)

feral kite
#

oh, that's not about constructors not inferring, that's about the constructor not being strongly typed, as the answer states

vestal oak
#

yes it why i use this pattern

feral kite
#

inference isn't really relevant there

vestal oak
#

my issu is on the red square 🟥

feral kite
#

because constructor cant infer
this is what you yourself said...

vestal oak
#

are you trolling me because my time is very precious !

feral kite
#

you do realize that communication is a two-way street
i asked you for clarification on an issue that you described incorrectly, which has now been clarified

vestal oak
#

i give you, I'm going to look elsewhere for help, obviously you're trolling me and am not understand you !

feral kite
#

im so confused by this thread lmao

#

well, i already said it's 1am. if you're just going to accuse me of trolling then yeah i don't think this is worth my time either.

wide yarrow
#

@vestal oak at the end of the day does your requirement reduce to this?

const f = <T extends { a: string }>(x: T) => x
f({
  a: 'hi',
  additionalProperty: true, // you want this to error?
})

(i may be confused but i don't think what you're asking about is specific to classes or constructors in any way)

vestal oak
# wide yarrow <@482309114622640152> at the end of the day does your requirement reduce to this...

yes, i find more info and some solution to play with, thanks.
i probably use bad wording to describe the issue TypeGuard function for a key in a generic object look the word am looking for.
https://stackoverflow.com/questions/69195889/typeguard-function-for-a-key-in-a-generic-object?rq=2
https://stackoverflow.com/questions/65241412/restrict-generic-key-type-based-on-lookup-type-in-typescript?rq=2

#

!resolve

raw swift
#

I'm curious, what's your use case? In your initial message, why wouldn't this work?

type Data = {
    a:number
    b:number
}

function foo1(o:Data) {}
vestal oak
vestal oak
# raw swift I'm curious, what's your use case? In your initial message, why wouldn't this wo...

in fact , all this will be not need if ts can just handle basic inference with constructor! but no so i need cheating with weird pattern


class SuperBase<const T=unknown> {
    constructor( public a:T ) {}
}

class A extends SuperBase {
    constructor() {
        super([1]); // infer [1] to SuperBase<[1]>
    }
    update() {
        this.a; // expected [1] and not unknow and juste because this i need complicate my architecture ! 
    }
}
feral kite
keen iceBOT
#
that_guy977#0

Preview:```ts
class SuperBase<const T=unknown> {
constructor( public a:T ) {}
}

class A extends SuperBase<[1]> {
constructor() {
super([1]); // infer [1] to SuperBase<[1]>
}
update() {
this.a; // expected [1] and not unknow and juste because this i need complicate my architecture !
...```