You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functionrepeat(/** @type {string} */message,/** @type {number} */times,){returnArray(times).fill(message).join(` `);}/** @type {Parameters<typeof repeat>[0]} */constmessage=`hello`;// Error: Type '"hello"' is not assignable to type 'never'. (ts2322)/** @type {Parameters<typeof repeat>[1]} */consttimes=3;// Error: Type '3' is not assignable to type 'never'. (ts2322)
🙁 Actual behavior
Parameters<typeof repeat> coalesces into type never.
🙂 Expected behavior
Parameters<typeof repeat> coalesces into type [message: string, times: number].
Additional information about the issue
This error occurs if a function's arguments are defined with @type, but not if they're explicitly defined with @param. For example, this works as expected:
/** * @param {string} message * @param {number} times */functionrepeat(message,times){returnArray(times).fill(message).join(` `);}/** @type {Parameters<typeof repeat>[0]} */exportconstmyMessage=`hello`;// No error/** @type {Parameters<typeof repeat>[1]} */exportconstmyTimes=3;// No error
However, it is unreasonable to always require that @params be explicitly written for every function, e.g. when writing a callback that already has a defined shape.
This utility type serves as a workaround:
exporttypeParams<Targetextends(...args: any)=>any>=Targetextends(arg0: infer Arg0,arg1: infer Arg1,arg2: infer Arg2)=>any// Can add more args if we need them
? [Arg0,Arg1,Arg2]
: never;
functionrepeat(/** @type {string} */message,/** @type {number} */times,){returnArray(times).fill(message).join(` `);}/** @type {Params<typeof repeat>[0]} */exportconstmyMessage=`hello`;// No error/** @type {Params<typeof repeat>[1]} */exportconstmyTimes=3;// No error
The text was updated successfully, but these errors were encountered:
🔎 Search Terms
paramaters param
🕗 Version & Regression Information
Parameters
⏯ Playground Link
https://www.typescriptlang.org/play/?strictNullChecks=true&filetype=js#code/GYVwdgxgLglg9mABAJwKYAdUEMoAoBQAkAPQBUpiAAlAJ6aIDeAzlMjGAOYC+ipxiAW1RMmWDqgA0RMhWp1UjMCAEAjVMh59EsIUykBKRkTRQQyJAEFkyLDVw7h+gHTAYAGze5do8c4BWcOy4AAaIwfoA3Phc+PgyVLT0DAAKWDZCUOpMADyJqHDAKBjYUAB8ANoADAC6msT4EAgsgsI+CgC8YQAWqB5wwVFx5AnyjKnpqJnIOXkFRZg4FQCMtbz1jWDNDkyInQDMUUA
💻 Code
🙁 Actual behavior
Parameters<typeof repeat>
coalesces into typenever
.🙂 Expected behavior
Parameters<typeof repeat>
coalesces into type[message: string, times: number]
.Additional information about the issue
This error occurs if a function's arguments are defined with
@type
, but not if they're explicitly defined with@param
. For example, this works as expected:However, it is unreasonable to always require that
@param
s be explicitly written for every function, e.g. when writing a callback that already has a defined shape.This utility type serves as a workaround:
The text was updated successfully, but these errors were encountered: