3

Right now, I'm doing:

function fn(arg) {
  const foo = arg as SomeType;
  ...
}

Is there a way to do the type casting in the function argument? I.e. something like function fn(arg as SomeType)

Here's an example:

Promise.race([
  Promise.resolve(1),
  new Promise((_, fail) => {
    setTimeout(fail, 1000);
  }),
])
  .then((res: number) => {
    ...
  });

But TS throws:

Argument of type '(res: number) => void' is not assignable to parameter of type '(value: unknown) => void | PromiseLike<void>'.
  Types of parameters 'res' and 'value' are incompatible.
    Type 'unknown' is not assignable to type 'number'

Instead, I have to do:

.then((temp) => {
  const res: number = temp as number;
  ...
});
3
  • 1
    How are you intending to use this function? Are you intending to pass it values that are not of type SomeType? Commented Feb 24, 2020 at 23:32
  • Did you find my answer to be complete? If so, please "accept" and upvote it so others can quickly find the answer and I can clear this from my support tracker. If not, let me know how else I can help! Commented Feb 25, 2020 at 23:29
  • @ShaunLuttin I added an example Commented Feb 28, 2020 at 21:25

3 Answers 3

2

In the example you're racing Promise<number> and Promise<unknown>, if you know that your second promise always fail (used for timeouts) you can type it as Promise<never>.

This would ensure that you cannot actually resolve the promise, as there is no possible value _ would accept.

Promise.race([
  Promise.resolve(1),
  new Promise<never>((_, fail) => {
    setTimeout(fail, 1000);
  }),
])
  .then((res: number) => {
    ...
  });
Sign up to request clarification or add additional context in comments.

Comments

1

TypeScript doesn't have "type casting" so much as "type asserting" since it doesn't change runtime behavior, at all. There's no way to "cast" a type in a function argument. However, you can "assert"/require it when developing with something like:

function fn(arg: SomeType) {
  ...
}

More info:

Type assertions are a way to tell the compiler “trust me, I know what I’m doing.” A type assertion is like a type cast in other languages, but performs no special checking or restructuring of data. It has no runtime impact, and is used purely by the compiler. TypeScript assumes that you, the programmer, have performed any special checks that you need.

6 Comments

This throws an error if the types don't match. I need to tell the compiler the argument type because it incorrectly infers the type of my parameter as unknown, but I know it's guaranteed to be a certain type.
@LeoJiang What code editor are you using? I know for sure that VS Code infers the type from arg: SomeType.
When I run tsc, it throws Argument of type 'foo' is not assignable to parameter of type 'bar'.(2345). It's fine if I manually cast it like in the original question
Specifically, Promise.race causes TS to think the argument type is unknown, but I know what it will always be.
@LeoJiang Without seeing the actual code, I'm not sure how much I can help. It may be beneficial to use arg: Partial<SomeType>
|
0
function fn(arg: SomeType) {
  ...
}

And if you want to be sure that the function is returning exactly te type of value you need, u should put it just after the argument, something like this:

function fn(arg: SomeType): SomeType {
  return const foo = arg;
}

This way you make sure the compiler tells you when the function is returning a value that is not of the type you wanted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.