#This should work right?

25 messages ยท Page 1 of 1 (latest)

lilac cloakBOT
#
brano.h#0

Preview:```ts
interface Base {
x: number
}

class Bob<T extends Base> {
meth(a: Base, b: Omit<T, keyof Base>): T {
return {
...b,
...a,
}
}
}```

high tinsel
#

no

mossy rose
#

Why?

lilac cloakBOT
#
that_guy977#0

Preview:```ts
interface Base {
x: number;
}

class Bob<T extends Base> {
meth(a: Base, b: Omit<T, keyof Base>) : T {
return {
...b,
...a
}
}
}

const specificBob = new Bob<{ x: 0 | 1 }>();
// T is { x: 0 | 1 }

const res = specificBob.meth({ x: 2 }, {}); // plain Base, any number is allowed
...```

high tinsel
#

as the error says:

'Omit<T, "x"> & Base' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Base'.

#

changing Base to Pick<T, keyof Base> generates the same error, though. im not sure what scenario would lead to that being an issue (at runtime)

mossy rose
#

Okay and can you somehow tell TS to accept only those specific keys?

high tinsel
#

wdym, it's the same keys

mossy rose
#

sorry specific types ๐Ÿ˜„

#

because 0|1 is subtype of number, how to disallow that

#

I need to make it work somehow... ๐Ÿ˜„ The code is working I just cant type it right

manic abyss
#

could you share the real use case? i'm having trouble thinking of when this setup would be useful/desirable. i could suggest alternatives but i dunno what's in or out of bounds

high tinsel
#

it doesn't really make sense with the type system

haughty grotto
#

Im working with Brano, basically what we are trying to do is to create a vue composable that returns multiple properties, as well as query with certain type that should always extend the PaginationRequestDTO interface. However the PaginationRequestDTO interface properties (pageNumber, pageSize) are constructed inside the composable and all the other properties specific to that certain type come as a parameter of the composable. So we tried to create the composable, but the query = ref(....) always gives us the mentioned error:

Argument of type 'Omit<QueryT, keyof PaginationRequestDTO> & { pageNumber: number; pageSize: number; }' is not assignable to parameter of type 'QueryT'.
'Omit<QueryT, keyof PaginationRequestDTO> & { pageNumber: number; pageSize: number; }' is assignable to the constraint of type 'QueryT', but 'QueryT' could be instantiated with a different subtype of constraint 'PaginationRequestDTO'.

manic abyss
lilac cloakBOT
#
mkantor#0

Preview:ts import { Ref, ref, reactive, watch, onMounted, } from "vue" import {useRoute, useRouter} from "vue-router" import { useResizeObserver, useFetch, } from "@vueuse/core" import type { PaginationRequestDTO, PaginationResponseDTO, } from "~/types/dtos" import {parsePaginationQueryParam} from "~/utils/pagination" ...

manic abyss
#

don't need the full/real code or anything, just enough to get the playground to show the error you're trying to fix

#

gonna step away for a bit, but will probably be back online later

haughty grotto
#

Sure, here's the updated code:

lilac cloakBOT
#
kesuera#0

Preview:```ts
import {
Ref,
ref,
reactive,
watch,
onMounted,
} from "vue"
import {useRoute, useRouter} from "vue-router"
import {
useResizeObserver,
useFetch,
} from "@vueuse/core"

interface PaginationRequestDTO {
pageNumber: number
pageSize: number
}

interface PaginationResponseDTO
...```

manic abyss
#

and you definitely care about having the caller be able to decide the specific type? or would just getting a PaginationRequestDTO be okay?

#

would this be workable?

lilac cloakBOT
#
mkantor#0

Preview:```ts
...
export const usePagination = <
DataT extends PaginationResponseDTO,
AdditionalQueryT

(
apiEndpoint: string,
additionalQuery: Ref<AdditionalQueryT>,
tableRef: Ref<any>,
prefixKey: string
) => {
const router = useRouter()
const route = useRoute()

const pageCount = ref(1)
const pageSizes = [5, 10, 15, 20]

const parsedPageNumber = parsePaginationQueryParam(
route.query[prefixKey + "PageNumber"] as string,
1
)
const parsedPageSize = parsePaginationQueryParam(
route.query[prefixKey + "PageSize"] as string,
10
)

const query = ref<
PaginationRequestDTO & Additional
...```

mossy rose
#

@haughty grotto