microsoft / microsoft/TypeScript
TS 3.4: Error when passing dynamically imported generic type as a function argument
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
TypeScript Version: 3.4.0-dev.201xxxxx
Search Terms: TS2322, dynamic import, dynamic import never, dynamic import generic
Code
.\node_modules\.bin\tsc --jsx react --lib es2017 --strict index.tsx
// ===== page.tsx ======
import * as React from 'react'
export interface PageProps {
title: string
}
export class Page extends React.Component<PageProps> {
render() {
return <div>{this.props.title}</div>
}
}
// ===== index.tsx =====
import * as React from 'react'
import{ PageProps, Page } from './page'
export function myFunction<TProps>(loader: () => Promise<React.ComponentType<TProps>>) {
}
// No error
myFunction(() => Promise.resolve(Page))
// No error
const loader: () => Promise<React.ComponentType<PageProps>> = () => import('./page').then(m => m.Page)
// Error
myFunction(() => import('./page').then(m => m.Page))
Expected behavior:
No compile error. This was the behavior in TS 3.3 and earlier.
Actual behavior:
There is an error after upgrading to TS 3.4:
index.tsx:14:18 - error TS2322: Type 'Promise<typeof Page | ComponentClass<never, any> | FunctionComponen
t<never>>' is not assignable to type 'Promise<ComponentType<PageProps>>'.
Type 'typeof Page | ComponentClass<never, any> | FunctionComponent<never>' is not assignable to type 'C
omponentType<PageProps>'.
Type 'ComponentClass<never, any>' is not assignable to type 'ComponentType<PageProps>'.
Type 'ComponentClass<never, any>' is not assignable to type 'ComponentClass<PageProps, any>'.
Type 'PageProps' is not assignable to type 'never'.
14 myFunction(() => import('./page').then(m => m.Page))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
index.tsx:4:44
4 export function myFunction<TProps>(loader: () => Promise<React.ComponentType<TProps>>) {
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The expected type comes from the return type of this signature.
Repro here: https://github.com/srmagura/ts-import-repro
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Reproduce el error con el comando tsc contra index.tsx y page.tsx, usando la importación dinámica y la función genérica que se muestran en el issue. Compara el comportamiento con TypeScript 3.3 y 3.4 y, después, sigue la inferencia genérica para el tipo de retorno Promise<ComponentType>. Se considera terminado cuando la llamada indicada compila sin el error TS2322, mientras que los ejemplos existentes siguen comportándose correctamente.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- react, typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bien especificado
- Aptitud para principiantes
- 35/100