'when should I explicitly import type in TypeScript?
I'm taking the type-challenge from https://github.com/type-challenges/type-challenges/blob/master/questions/898-easy-includes/README.md,and I find something interesting and confusing: Why files in the same directory could use type without import and export ? My project is like :
and I define sometype in template.ts
// template.ts
type Includes<T extends unknown[], K> = T extends [infer First, ...infer Rest]
? K extends First
? true
: Includes<Rest, K>
: false
and in the some directory ,
// test-cases.ts
import { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<
Equal<Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Kars'>, true>
>,
Expect<
Equal<Includes<['Kars', 'Esidisi', 'Wamuu', 'Santana'], 'Dio'>, false>
>,
Expect<Equal<Includes<[1, 2, 3, 5, 6, 7], 7>, true>>,
Expect<Equal<Includes<[1, 2, 3, 5, 6, 7], 4>, false>>,
Expect<Equal<Includes<[1, 2, 3], 2>, true>>,
Expect<Equal<Includes<[1, 2, 3], 1>, true>>,
Expect<Equal<Includes<[{}], { a: 'A' }>, false>>,
Expect<Equal<Includes<[boolean, 2, 3, 5, 6, 7], false>, false>>,
Expect<Equal<Includes<[true, 2, 3, 5, 6, 7], boolean>, false>>,
Expect<Equal<Includes<[false, 2, 3, 5, 6, 7], false>, true>>,
Expect<Equal<Includes<[{ a: 'A' }], { readonly a: 'A' }>, false>>,
Expect<Equal<Includes<[{ readonly a: 'A' }], { a: 'A' }>, false>>,
Expect<Equal<Includes<[1], 1 | 2>, false>>,
Expect<Equal<Includes<[1 | 2], 1>, false>>,
Expect<Equal<Includes<[null], undefined>, false>>,
Expect<Equal<Includes<[undefined], null>, false>>
]
you can see that I didn't export type Includes and neither import it , In test-cases.ts file I could use it .
but if change the template.ts with importing some util type from other directory:
// template.ts
import { Equal } from '@type-challenges/utils'
type Includes<T extends unknown[], K> = T extends [infer First, ...infer Rest]
? Equal<First, K> extends true
? true
: Includes<Rest, K>
: false
I got error in test-cases.ts,it says can't find the name Includes
could someone explain why is that?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

