Why not T | undefined? What is a good way to override this behavior.
Also, I am looking for a good series on Vue TypeScript. Recently I got this behavior solved, $mq.
Mostly the problem is always, extending incomplete typed libraries, or JSDoc is lost, and VSCode showing the useless, declare module '...' instead. (No, they are not telling how to properly write declaration.d.ts or even src/@types/.../index.d.ts. They don't even provide a link, only No quick fixes available.).
The other way round is problematic too. I cannot write JSDoc @type from TypeScript types / interfaces.
functionobjGet<T,KextendskeyofT>(o:T,k:K){returno[k];}consta=[1,"Test",{id:1,name:"test"}];constUser={id:1,name:"Test User",points:20};constid=objGet(User,"id");// numberconstpoints=objGet(User,"points");// numberconstuserName=objGet(User,"name");// string// const somethingElse = objGet(User, "nonExistent");// !Error// Argument of type '"nonExistent"' is not assignable to parameter of type '"id" | "name" | "points"'.
We can limit the possible lookup keys by using keyof T, which would prevent calling any undefined keys. This would also ensure that the correct type is written for any provided lookup key.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I am forever bugged by
Why not
T | undefined
? What is a good way to override this behavior.Also, I am looking for a good series on Vue TypeScript. Recently I got this behavior solved, $mq.
Mostly the problem is always, extending incomplete typed libraries, or JSDoc is lost, and VSCode showing the useless,
declare module '...'
instead. (No, they are not telling how to properly writedeclaration.d.ts
or evensrc/@types/.../index.d.ts
. They don't even provide a link, onlyNo quick fixes available.
).The other way round is problematic too. I cannot write JSDoc
@type
from TypeScript types / interfaces.Thanks for the great example and feedback! Don't know too much about Vue, but will definitely have a look at the example you posted.
Here is a solution for the problem with
objGet
:We can limit the possible lookup keys by using
keyof T
, which would prevent calling any undefined keys. This would also ensure that the correct type is written for any provided lookup key.