简体   繁体   中英

TypeScript generic type with unknown

What does this statement mean?

type O<T> = T | unknown[]

In which case is unknown used and what does O<T> mean?

Full description of unknown . https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-0.html#new-unknown-top-type

In short, anything can be assigned to unknown , but access to unknown requires type narrowing.

O<T> from your example means some T or array of unknowns.

let o: O<number> = 123;
o = [true, '123'];
if (typeof o === 'number') {
  const n: number = o;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM